我是一名新的计算老师,我在使用此代码时遇到了一些问题。我试图让我的学生使用while和if语句创建一个非常简单的游戏。
当我运行此代码时,我不会输入y或n,它会一直出现错误。有什么想法吗?
setInterval(function(){
....
}, 3000);
答案 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