Python:获取输入时发生Unicode错误

时间:2019-11-12 02:02:50

标签: python-3.x syntax-error

所以在尝试运行程序时,我总是收到此错误:

文件“ C:/用户/使用/.spyder-py3/minip.py”,第277行     get_input = input(“ Continue?Y \ N:”) SyntaxError:(unicode错误)“ unicodeescape”编解码器无法解码位置11-12的字节:格式错误的\ N字符转义

我不知道为什么会给我一个错误,一切对我来说似乎都是正确的。 只需将其放到那里,我就进入编码课程只有几周了,所以我在任何方面都不是很高级 ^

        p1.betWin(betInput, otherBet)
        print("You won:$",otherBet + betInput)
        p1.show()
        p2.show()
        get_input = str(input("Continue? Y\N:"))
        continueGame(get_input)

continueGame的定义

        if get_input == 'y' or get_input == 'Y' or get_input == 'yes' or get_input == 'Yes':
            run = True
            return run
        if get_input == 'n' or get_input == 'N' or get_input == 'no' or get_input == 'No':
            run = False
            return run

2 个答案:

答案 0 :(得分:1)

\字符称为"escape" character,用于对诸如换行符(\n)之类的字符进行编码。如果您想使用文字\,则应该这样做:

"Continue? Y\\N:"

答案 1 :(得分:0)

这是一个简单的(如错误所述)语法问题,它试图从使用Y \ N(是或否)的用户那里获取输入,但忘记了\ n的使用。