當我運行代碼時,它要求用戶輸入3次,但是當條件滿足時,它仍然只打印其中的2個。爲什麼?我對Python很陌生,但在谷歌上看到任何答案都很瘋狂,但我仍然感到困惑。在while循環中分配後只有兩個變量
name = raw_input("Name your age: ")
print ("3 choices.")
key = raw_input("What will this world have?: ")
def generator():
data = 1
choice1 = ""
choice2 = ""
choice3 = ""
while(data != 3):
key = raw_input("What will this world have?: ")
data += 1
if (key == "grass"):
choice1 = "The world is covered in green grass."
elif (key == "water"):
choice2 = "The world is covered in water."
elif (key == "sky"):
choice3 = "The sky is vast and beautiful."
if (data >= 3):
print("Before you is " + name)
print(choice1)
print(choice2)
print(choice3)
raw_input("Press Enter to continue...")
else:
print("Invalid Option")
generator()
你增加'data'計數的用戶輸入後,無論如果它是一個有效的選擇與否。如果你只在* if內增加'data'('data + = 1'),那麼你只能計算有效的數據' – davedwards