NameError出现但我不明白为什么

时间:2017-11-26 00:02:34

标签: python nameerror

我为学校项目编写了一些代码,但它显示了我的代码的所有成本部分的错误

fullname = input("whats your name?")
print("hello",fullname,)
print("room 1")
width=input("how wide is the room?")
length=input("how long is the room?")
area = float(length)*float(width)
print("the total area is",area,)

q = input("do you want another room?")
if q == "yes":
   print("room 2 ")
   widtha=input("how wide is the room?")
   lengtha=input("how long is the room?")
   areaa = float(lengtha) * float(widtha)
   print("the total area is",areaa,".")
else:
   flooring=input("do you want to have wood flooring(10) or tiled flooring(15)")
   if flooring == "wood":
      costaa = float(area)+ float(areaa)*10
      print("total cost is ",costaa,)
   if flooring == "tiled":
      costab = float(area)+ float(areaa)*15
      print("total cost is £",costab,)

根据我选择的内容,它会显示NameError:costabNameError:costaa未定义。

2 个答案:

答案 0 :(得分:0)

您的问题的答案很简单。 当电脑说:你想要另一个房间吗?而且你说不,你根本没有定义areaa的价值。因此,areaa不存在,因为您没有指定它。我已经对你的代码进行了一些更改以使其正常工作,希望它有所帮助!

fullname = input("Whats Your Name?")
print("Hello", fullname)
print("\nRoom 1")

width = input("How wide is the room?")
length = input("How long is the room?")

area = float(length) * float(width)
print("The Total Area is", area)

q = input("\nDo you want Another Room? (y/n)")

if q == "y":
    print("\nRoom 2")
    width2 = input("How wide is the room?")
    length2 = input("How long is the room?")
    area2 = float(length2) * float(width2)
    print("The Total Area is", area2,".")

    flooring=input("\nDo you want to have Wood Flooring(10) or Tiled Flooring(15)? (wood/tiled)")
    if flooring == 'wood':
        cost = area + area2 * 10
        print("Total Cost is ",cost)
    if flooring == 'tiled':
        cost2 = area + area2 * 15
        print("Total Cost is ",cost2)

else:
    flooring=input("\nDo you want to have Wood Flooring(10) or Tiled Flooring(15)? (wood/tiled)")
    if flooring == 'wood':
        cost = area * 10
        print("Total Cost is ",cost)
    if flooring == 'tiled':
        cost2 = area * 15
        print("Total Cost is ",cost2)

答案 1 :(得分:-1)

我已经测试了你的代码并在我的电脑上运行正常。也许这是你的配置。

对于python 2

input_variable = raw_input(“输入你的名字:”)

如果您使用 Python 3.x ,则raw_input已重命名为输入