用户输入不存储在变量中

时间:2015-11-16 10:27:23

标签: python

我是一名新的计算老师,我在使用此代码时遇到了一些问题。我试图让我的学生使用while和if语句创建一个非常简单的游戏。

当我运行此代码时,我不会输入y或n,它会一直出现错误。有什么想法吗?

setInterval(function(){
    ....
}, 3000);

3 个答案:

答案 0 :(得分:4)

您正在将playerDecision变量 y变量 n进行比较。

您应该将其更改为字符串 "y""n"

if playerDecision == "y":
if playerDecision == "n":

答案 1 :(得分:2)

您得到的错误是NameError,因为未定义变量y。 你真正想做的是

if playerDecision == 'y':
    print ("You attack the monster and do 5 damage")
if playerDecision == 'n':

答案 2 :(得分:0)

playerDecision = input("Would you like to stay and fight? y/n")

if playerDecision == 'y':  # <-- this should fix it. 
    print ("You attack the monster and do 5 damage")
if playerDecision == 'n':
    print("You run away with your tail between your legs.")
    break     

if playerHealth <= 0:
    print ("You died......")
    break   

if monsterHealth <= 0:
    print ("You defeated the monster!")
    break