当我运行代码时,它要求用户输入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