【发布时间】:2016-09-15 05:51:05
【问题描述】:
我正在编写一个程序,告诉用户输入一个属性的房间数量,然后一个 while 循环找出每个房间的宽度和长度。我觉得我需要while循环来创建两个额外的变量来存储每次迭代时的宽度和长度,但是我不知道如何。 到目前为止,这是我的代码:
roomCount = 1
print("Please answer the questions to find the floor size.")
rooms = int(input("How many rooms has the property got?:\n"))
while roomcount >= rooms:
print("For room", roomcount,", what is the length?:\n")
不多,但我一直在网上搜索,但没有找到方法。
我希望程序做的是询问用户该物业有多少个房间,然后对于每个房间,它应该询问房间的宽度和长度。然后,程序应该以用户友好的格式显示建筑空间的总面积
更新代码:
currentRoomNumber = 0
currentRoomNumber2 = 0
floorspace = 0
whileLoop = 0
print("Please answer the questions to find the floor size.")
numberOfRooms = int(input("How many rooms has the property got?: "))
roomWidths= list()
roomLengths = list()
while currentRoomNumber < numberOfRooms:
roomWidths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the width?: ")))
roomLengths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the length?: ")))
currentRoomNumber += 1
while whileLoop < numberOfRooms:
floorspace += (roomLengths[(currentRoomNumer2)] * roomWidths[(currentRoomNumber2)])
currentRoomNumber2 += 1
whileLoop += 1
print(floorspace)
但是,在输入房间尺寸的值后,它在第 15 行给我一个回溯错误,并说 currentRoomNumber2 未定义。我哪里出错了?
【问题讨论】:
-
room("For room", roomcount,", what is the length?:\n")这是一个函数调用吗,因为它看起来很像。你想在这里做什么? -
这是我的大脑工作不正常,抱歉。它的意思是打印
-
您是否尝试在每次循环时增加
roomcount?如果是这样就去roomcount += 1 -
你还有两个不同的变量,
roomCount和roomcount,我认为你的 while 表达式应该看起来像roomcount <= rooms。另外,不要忘记在每次迭代后增加roomcount。 -
给我们一个测试用例,并清楚地解释你想要什么。
标签: python variables while-loop