python - 为什么在这种特殊情况下没有定义 y ?

标签 python python-2.7

我是 Python 2.7 的新手,我尝试创建一个简单的骰子游戏,但是我在让计算机识别 y 时遇到了一些困难,因为无论我做什么,它都不会将答案识别为 y 或 n。这是下面的代码。

import random
a = random.randint(1, 6)
b = random.randint(1, 6)
points = 0
strikes = 0

def main():
    print """Welcome to the two dice game. Depending on the result, you will earn a point or a strike. Three strikes end the game. Good luck!"""
    anwser = input('Play? y for yes, n for no')
    if anwser == "y":
       print a
       print b
       if a == b:
          print ("Congrats! You earned a point!")
          points = points + 1
          main()
    else:
        print("That's a strike...")
        strikes = strikes + 1
        if strikes == 3:
            print("That's three strikes, game over.")
            break
        else:
            main()
    if anwser == n:
       print ("Game over. You earned this many points.")
       print points

main()

最佳答案

input() 实际上会尝试评估您传递给它的任何内容。您需要 raw_input()

参见 this question有关 input()raw_input() 的更多信息。

关于python - 为什么在这种特殊情况下没有定义 y ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31056077/

相关文章:

python - 从变量 Python (urllib2.urlopen) + Beautifulsoup4 打开链接

python - 使用 Glob 将文件夹内部分名称的文件名获取到列表

python - 当我制作一个 pygame 游戏并将鼠标悬停在它上面时,光标变成正在加载的东西

python - 如何动态代理类的方法?

virtualenv : broken `site.py` 下的 Python 2.7

python 库从网站获取内容(http 文本)?

python - 用python计算员工字典数据库中的值

python - 如何在 cherrypy 应用程序中使用 ajax 调用 python 脚本

python - 如何转换 matplotlib ConnectionPatch,即用于 Cartopy 投影

python - 方法 set_seq1 和 set_seq2 的工作,difflib python