我正在編寫一個程序,告訴用戶輸入屬性的房間數量,然後while循環找出每個房間的寬度和長度。我覺得我需要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
未定義。我哪裏錯了?
'房間(「房間」,房間數量,「長度是多少」:\ n「) 這是一個函數調用,因爲它確實看起來像一個函數調用。你想在這裏做什麼? – MooingRawr
這是我的大腦不能正常工作,對不起。它的意思是說打印 – Student
你想每次增加while循環時增加'roomcount'嗎?如果是的話,就去'roomcount + = 1' – MooingRawr