2014-07-17 18 views
2

我有一個任務要求用戶有多少個方向他們有一個Finch機器人在Python中。 在我有多少方向之後,我需要詢問並保存其中的每一個。 我無法弄清楚的是如何切換我正在使用的變量並仍然存儲用戶的答案。如何更改正在使用的變量?

count = 1 

nod = int(input("How many directions do you have?: ")) #nod = number of directions 
for int in range(0, nod): 
    d1 = str(input("Direction " + str(count) + ": ")) 
     #I want to switch out d1 with d2,d3, etc. for as many directions as the user has 
count += 1 
+0

你使用Python 3嗎? –

回答

5

在這種情況下,不要使用單一變量,但名單:

directions = [] 
nod = int(input("How many directions do you have?: ")) #nod = number of directions 
for i in range(nod): 
    directions.append(input("Direction {}: ".format(i+1))) 

如果你使用Python 3,你不需要調用str()input()的結果。如果您使用的是Python 2,請改爲使用raw_input()

請注意,您不需要count變量,因爲nod已經包含該信息。 (你可以隨時撥打len(directions))。請注意,由於Python的計數從0開始,所以第一個方向是direction[0]