2016-09-14 4 views
0

私はプロパティを持つ部屋の量を入力するようにユーザーに指示し、whileループは各部屋の幅と長さを調べるプログラムを書いています。私はwhileループを必要としているので、反復するたびに幅と長さを格納するために2つの余分な変数を作成する必要があるように感じます。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] 

必ずあなたはとしてそれを参照します意味し、部屋の「0」として部屋「1」(長さ1)を参照:

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

、私はこのようなものだろう:部屋1の長さを得るためにその後

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} 

を、ちょうどそれを調べる: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

関連する問題