Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么在这种特殊情况下不定义y?_Python_Python 2.7 - Fatal编程技术网

Python 为什么在这种特殊情况下不定义y?

Python 为什么在这种特殊情况下不定义y?,python,python-2.7,Python,Python 2.7,我对Python2.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, y

我对Python2.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()
实际上尝试计算您传递给它的任何内容。您需要
原始输入()

有关
input()
raw\u input()

input()
的详细信息,请参阅。您需要
原始输入()


有关
input()
raw\u input()

的更多信息,请参阅。我发现,使用Codecademy中所教的raw\u input,使用python 2获取输入效果很好。在Python3中,其input()函数运行良好。在main()中访问应该声明为全局的击数和点数时出现问题,最好将a和b放入main()中,以便用户在main()自身重新运行时重新掷骰子,否则他们会被相同的掷骰子卡住,该掷骰要么在3次玩中终止,要么永远不会终止。if-then逻辑可以使用一些修正,使“n”答案终止游戏,如下所示,以及一些其他更改,如格式化初始消息,使其不会超出正常大小的窗口:

import random
points = 0
strikes = 0

def main():
    a = random.randint(1, 6)
    b = random.randint(1, 6)
    global points
    global strikes
    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!"""
    answer = raw_input('Play? y for yes, n for n: ')
    if answer == "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.")
                exit(1)
            else:
                main()
    elif answer == "n":
        print ("Game over. You earned this many points.")
        print points

main()
通过将罢工计算为罢工-=点,并在终止时使用“n”将罢工点偏移为罢工-=点,用点偏移罢工点,这可能很有趣


Codecademy为Python 2提供了免费的所有实践培训。我最近经历了它,它很好。对于Python3来说,Bill Lubanovic介绍Python是非常好的,因为它从初级水平开始,发展到中级专业水平,可读性很强,在章节末尾有练习,在附录中有解决方案,在github上有代码,与一些Python大部头相比,它相对较短(例如学习Python和编程Python)--它在200-250页的7-9章中涵盖了核心Python 3,其余部分则在更专业、更可选但有趣的领域,如NoSQL数据库访问、web服务和样式。

我发现,使用Codecademy教授的原始输入,使用Python 2获取输入效果很好。使用Python 3,它的输入()函数运行良好。在main()中访问应该声明为全局的击数和点时出现问题,最好将a和b放入main()中,以便用户在main()时重新掷骰子重新运行本身,否则他们会被相同的卷卡住,要么在3次播放中终止,要么永远不会终止。if-then逻辑可以使用一些修正,以便“n”回答按照以下建议终止游戏,以及一些其他更改,例如格式化初始消息,使其不会超出正常大小的窗口打印:

import random
points = 0
strikes = 0

def main():
    a = random.randint(1, 6)
    b = random.randint(1, 6)
    global points
    global strikes
    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!"""
    answer = raw_input('Play? y for yes, n for n: ')
    if answer == "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.")
                exit(1)
            else:
                main()
    elif answer == "n":
        print ("Game over. You earned this many points.")
        print points

main()
通过将罢工计算为罢工-=点,并在终止时使用“n”将罢工点偏移为罢工-=点,用点偏移罢工点,这可能很有趣


Codecademy为Python 2提供了免费的所有实践培训。我最近完成了它,而且它很好。对于Python 3,由Bill Lubanovic介绍Python是非常好的,因为它从初级水平开始,发展到中级专业水平,可读性很强,在章节末尾有练习,在附录中有解决方案,在github上有代码与一些Python巨著(例如学习Python和编程Python)相比,它的篇幅相对较短——它以200-250页的篇幅在7-9章中涵盖了核心Python 3,其余的则是更专业、可选但有趣的领域,如NoSQL数据库访问、web服务和样式。

正确的拼写是“答案”(只是想让您知道)。您需要使用原始输入而不是输入,因为它是python 2x而不是3x。另一件需要注意的事情是,在
main
上递归可能不是您想要做的事情。请尝试使用
while
循环(
def main():while stricks<3:
)您使用三种不同类型的打印语句格式:一种在开头有三个引号,一种没有括号,另一种有。我建议您将所有
print
方法都用括号括起来,这样比较一致,因为在python3中移植和运行起来更容易。例如:
print(a)
还有您的“main()”底部的部分应该有这样的内容:哇,谢谢你的帮助。我没有意识到我把事情搞砸了。不是搞砸了。学习机会。你在使用Python 2的教程吗?如果你刚刚开始,为什么不学习Python 3呢?正确的拼写是“答案”(只是让你知道)。您需要使用原始输入而不是输入,因为它是python 2x而不是3x。另一件需要注意的事情是,在
main
上递归可能不是您想要做的事情。请尝试使用
while
循环(
def main():while strokes<3:
)您使用三种不同类型的打印语句格式:一种在开头有三个引号,一种没有括号,另一种有。我建议您将所有
print
方法都用括号括起来,这样比较一致,因为在python3中移植和运行起来更容易。例如:
print(a)
还有您的“main()”底部的部分应该有这样的内容:哇,谢谢你的帮助。我没有意识到我把事情搞砸了。不是搞砸了。学习机会。你在使用Python 2的教程吗?如果你刚刚开始,为什么不学习Python 3呢?顺便说一句,我现在正在做Codecademy。我只是试图应用我学到的东西,这显然是没什么。顺便说一句,我现在正在做编解码器。我只是想把我学到的东西应用到实际工作中,但显然不多。