2016-09-14 62 views
0

我正在編寫一個程序,告訴用戶輸入屬性的房間數量,然後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未定義。我哪裏錯了?

+3

'房間(「房間」,房間數量,「長度是多少」:\ n「) 這是一個函數調用,因爲它確實看起來像一個函數調用。你想在這裏做什麼? – MooingRawr

+0

這是我的大腦不能正常工作,對不起。它的意思是說打印 – Student

+0

你想每次增加while循環時增加'roomcount'嗎?如果是的話,就去'roomcount + = 1' – MooingRawr

回答

1

從你的問題,好像這是你想要的東西:

print("Please answer the questions to find the floor size.") 
    numberOfRooms = int(input("How many rooms has the property got?: ")) 
    currentRoomNumber = 0 
    roomLengths = list() 
    while currentRoomNumber < numberOfRooms: 
     roomLengths.append(int(input("For room " + str(currentRoomNumber + 1) + ", what is the length?: "))) 
     currentRoomNumber += 1 
    print roomLengths 

這使得每個房間的長度到一個列表(記住房「1」,根據用戶房間「0」給你)。

當你運行它,它看起來像這樣(我把每個房間的長度,無論房間數):

Please answer the questions to find the floor size.                                     
    How many rooms has the property got?: 5                                        
    For room 1, what is the length?: 1                                         
    For room 2, what is the length?: 2                                         
    For room 3, what is the length?: 3                                         
    For room 4, what is the length?: 4                                         
    For room 5, what is the length?: 5                                         
    [1, 2, 3, 4, 5] 

訪問每個房間的長度,就像我之前說的,做確保您資料室「1」(長度1)房「0」,這意味着你將其引用爲:

print roomLengths[0] 

這可能是簡單的,但我想明確告訴你,因爲你是問如何到「創建變量」,真正想要的是一個列表,因爲你不知道你會想要多少「變量」創建;所以這個列表可以有你需要的很多房間長度。

要在裏面添加的寬度,你想補充另一個列表和輸入像這樣:

print("Please answer the questions to find the floor size.") 
    numberOfRooms = int(input("How many rooms has the property got?: ")) 
    currentRoomNumber = 0 
    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 
    print "Room widths:" 
    print roomWidths 
    print "Room lengths:" 
    print roomLengths 

輸出/腳本的運行會則是這樣的:

Please answer the questions to find the floor size.                                     
    How many rooms has the property got?: 3                                        
    For room 1, what is the width?: 1                                          
    For room 1, what is the length?: 2                                         
    For room 2, what is the width?: 3                                          
    For room 2, what is the length?: 4                                         
    For room 3, what is the width?: 5                                          
    For room 3, what is the length?: 6                                         
    Room widths:                                               
    [1, 3, 5]                                                
    Room lengths:                                               
    [2, 4, 6] 
+0

每間房間的房間寬度是多少? 'floorArea = roomWidths [0] * roomLengths [0]'或類似的東西? – Student

+0

的確如此。 –

+0

@PavelGurkov我知道這就是我現在要檢查它的方式,但是我怎樣才能檢查每個房間? – Student

0

思考你想要的長度和寬度由房號被保存,我會做這樣的事情:

rooms = {} 

print("Please answer the questions to find the floor size.") 

rooms = int(input("How many rooms has the property got?:\n")) 

for room_num in range(1, rooms+1): 
    length = int(input("What is the lenth of room number {}: ".format(room_num))) 
    width = int(input("What is the lwidth of room number {}: ".format(room_num))) 
    rooms[room_num] = {'length': length, 'width': width} 

然後拿到房間1的長度,只是看它:rooms[1]['length'],寬度:rooms[1]['width']等。等

0

我建議去一個Room類。然後你可以定義一個area方法。

class Room(): 
    def __init__(self, w, l): 
     self.width = w 
     self.length = l 

    def area(self): 
     return self.width * self.length 

然後,爲了您的輸入,將它們存儲到列表中。改爲使用for-loop。不需要while循環。

print("Please answer the questions to find the floor size.") 

roomCount = int(input("How many rooms does the property have?\n")) 

rooms = [] 
for roomNum in range(roomCount): 
    l = float(input("For room {} what is the length?\n".format(roomNum + 1))) 
    w = float(input("For room {} what is the width?\n".format(roomNum + 1))) 
    rooms.append(Room(w, l)) 

當你完成了這一點,你只需循環對象和合並區域。

size = sum(r.area() for r in rooms) 
print(size) 
0

我會這樣做。

rooms_total = int(input('How many rooms?')) 
rooms_info = list() 

for i in range(rooms_total): 
    length = int(input('Length for room #{}?'.format(i))) 
    width = int(input('Width for room #{}?'.format(i))) 
    rooms_info.append({'length': length, 'width': width}) 

space = sum([x['length'] * x['width'] for x in rooms_info]) 
print(space) 

類感覺就像矯枉過正(除非您特別想練習班),和字典不覺得自己是一個合適的外部數據結構在這裏。

這只是我的愚見,但你需要閱讀更多關於Python中的循環和基本數據結構。

+0

我同意你的看法,但我只在中學,並且在過去的6個星期裏一直沒有做任何編碼。對不起 – Student