Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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 抛硬币程序中的名称错误_Python_Python 2.7 - Fatal编程技术网

Python 抛硬币程序中的名称错误

Python 抛硬币程序中的名称错误,python,python-2.7,Python,Python 2.7,这是我为一个简单的掷硬币程序编写的代码。唯一的问题是没有定义h和t,尽管我希望它们是userChoice变量。 我错过了什么或做得不对 class Heads_or_Tails: import random print("Enter 'h' for Heads and 't' for Tails.") userChoice = raw_input() userChoice = raw_input() try: input("Press

这是我为一个简单的掷硬币程序编写的代码。唯一的问题是没有定义h和t,尽管我希望它们是userChoice变量。 我错过了什么或做得不对

class Heads_or_Tails:
    import random

    print("Enter 'h' for Heads and 't' for Tails.")
    userChoice = raw_input()
    userChoice = raw_input()

    try:
        input("Press Enter to see result.")
    except SyntaxError:
        pass

    a = 1
    b = 2

    machineChoice = random.randrange(1, 2, 2)

    if(machineChoice == a):
        if(userChoice == h):
            print("You win!")
        elif(userChoice == t):
            print("You lose.")
        else:
            pass
    if(machineChoice == b):
        if(userChoice == h):
            print("You lose.")
        if(userChoice == t):
            print("You win!")
        else:
            pass

    try:
        input("Press Enter to exit.")
    except SyntaxError:
        pass

ifuserChoice==h:应该是if userChoice='h':我刚刚尝试了这个,我的程序没有显示任何结果。这是结果…输入“h”表示正面,输入“t”表示反面。h按Enter键查看结果。按回车键退出。>>>不过,谢谢你的建议!哎呀!我有两次userChoice=raw\u输入。现在我将重试你的建议。最终的代码是134个字符太长了。我将random.randrange1,2,2更改为random.randinta,b,并删除了其中一个userChoice=raw\u输入命令。非常感谢你!