2016-09-11 6 views
-1

forループを作成して、セットのユーザーデータをすばやく取得します。データを有効に保つには、それぞれの変数に保存する変数が必要です。これまでのところ、私は持っています:変数が増加するループの場合

hey = ['11', '12', '13', '14'] 
x = 0 
for i in hey: 
    x += 1 
    showtimesx = raw_input('NOG >') 
    print showtimesx 

print "" 
print "Showtime at 11: " + showtimesx 
print "Showtime at 12: " + showtimesx 
print "Showtime at 13: " + showtimesx 
print "Showtime at 14: " + showtimesx 

最終的には、showtimesxの価値が高まったことを確認するだけです。しかし、私がそれを実行するたびに、それらのすべてが最後に入力された値と等しくなります。

私はx + = 1行を移動しようとしましたが、ループの中にx = 0行とその他の一連の行がありますが、どれも動作しませんでした。

どうすれば修正できますか?

+2

あなたは、コードのこれらの行の間で変更するには、 'showtimesx'の値を引き起こすと思いますか? – hobbs

+2

プログラムを書くとき、事態が順番に起こります。あなたのコード中の 'showtimesx'に対する全ての変更は、それを表示しようとする試みの前に発生し、単一の値を格納します。結果は驚くべきことではありません。ヒント: 'hey'は複数の値を格納します。多分、あなたは 'showtimesx'に複数の値を保存するために同様のテクニックを使うことができます。 –

+0

あなたはおそらく[Dictionary](http://www.tutorialspoint.com/python/python_dictionary.htm) – ti7

答えて

0

あなたがリストを使用したい:

hey = ['11', '12', '13', '14'] 
showtimes = [raw_input('NOG >') for i in hey] 

print "" 
print "Showtime at 11: " + showtimes[0] 
print "Showtime at 12: " + showtimes[1] 
print "Showtime at 13: " + showtimes[2] 
print "Showtime at 14: " + showtimes[3] 
+0

ソリューションをありがとうございます。それは実際に動作します! :) –

0

理由は、あなたのforが行う各ループのために、showtimesxが上書きされてしまうことです。 このコード打撃を助ける:

hey = ['11', '12', '13', '14'] 
x = 0 
showtimesx = [] 
for n in hey : 
    for i in n: 
     x += 1 
     showtimesx.append(input('NOG >')) #Adds the user's input too end of showtimesx            
     print (showtimesx)    #FYI: input() is equal to raw_input() in python3 
     break 

print ("") 
print ("Showtime at 11: " + showtimesx[0]) 
print ("Showtime at 12: " + showtimesx[1]) 
print ("Showtime at 13: " + showtimesx[2]) 
print ("Showtime at 14: " + showtimesx[3]) 
0

リストアプローチあなたはを反復処理し、各値を使用して操作を実行する必要がある場合は、enumerate(), which will return both the value from your list and its position in the listを試してみてください


これにより、リスト内の値をインデックスで変更することもできます。あなたのケースでは

mylist = ['11', '12', '13', '14'] 

for index, value in enumerate(mylist): 
    if int(value) == 12: 
     mylist[index] = "fifteen instead" 

print mylist # ['11', 'fifteen instead', '13', '14'] 

辞書アプローチ

consider using a dictionary。これにより、より簡単にそれらを保存し、mylist[1]のようなインデックスを覚えておくか、値を見つけるまでそれを検索するのではなく、名前( "キー")で後でそれらを検索することができます。

>>> colors = {"pineapples": "sorry, no pineapples"} # initial key: value pairs 
>>> colors["red"] = "#FF0000" # add value 
>>> print colors["red"] # retrieve a value by key 
#FF0000 

はここにあなたのケースの完全な機能として例を示します

def showtimes(list_of_times, display=True): 

    dict_of_times = {} # empty dictionary to store your value pairs 

    print "please enter values for the {} showtimes".format(len(list_of_times)) 
    for value in list_of_times: 
     dict_of_times[value] = raw_input("NOG > ") 

    if display: # Let the user decide to display or not 
     print "" 
     for time in sorted(dict_of_times.keys()): # dictionaries are unsorted 
      print "Showtimes at {}: {}".format(time, dict_of_times[time]) 

    return dict_of_times # keep your new dictionary for later 


hey = ['11', '12', '13', '14'] 
showtimes(hey) # pass a list to your function 
関連する問題