SyntaxError:EOL在扫描字符串文字Python计算器时

SyntaxError:EOL在扫描字符串文字Python计算器时,python,python-3.x,Python,Python 3.x,这是我用Python 3.3.0编写的计算器 这是我的节目 import random import math a=int(input('Please enter your first number!: ')) x=int(input('Please enter your second number!: ')) menu='So what do you want me to do with these numbers? (Ps. Only put in the number)\n\ 1.

这是我用Python 3.3.0编写的计算器 这是我的节目

import random
import math
a=int(input('Please enter your first number!: '))
x=int(input('Please enter your second number!: '))
menu='So what do you want me to do with these numbers? (Ps. Only put in the number)\n\
    1. Do you want me to add the numbers together?\n\
    2. Do you want me to subtract the numbers?\n\
    3. Do you want me to multipy the numbers?\n\
    4. Do you want me to divide the numbers?\n\
    5. Do you want me to square the numbers?\n\
    6. Do you want me to put one number to the power of the other?\n\
    7. Do you want me to square root both numbers?\n\
    8. Nothing but quit!\n\'
y=int(input(menu))
if y==1:
    print(str(a)+' + '+str(x)+' = '+str(a+x))
elif y==2:
    c=int(input('Which number will you subract from? 1. '+str(a)+' or 2. '+str(x)+'? (Remember only put 1 or 2) '))
    if c==1:
        print(str(a)+' - '+str(x)+' = '+str(a-x))
    elif c==2:
        print(str(x)+' - '+str(a)+' = '+str(x-a))
elif y==3:
    print(str(a)+' x '+str(x)+' = '+str(a*x))
elif y==4:
    d=int(input('Which number will you divide from? 1. '+str(a)+' or 2. '+str(x)+'? (Remember only put 1 or 2) '))
    if d==1:
        print(str(a)+' ÷ '+str(x)+' = '+str(a/x))
    elif d==2:
        print(str(x)+' ÷ '+str(a)+' = '+str(x-a))
elif y==5:
    b=int(input('Which number do you want to square? 1. '+str(a)+' or 2. '+str(x)+'? (Remember only put 1 or 2) '))
    if b==1:
        print(str(a)+' x '+str(a)+' = '+str(a*a))
    elif b==2:
        print(str(x)+' x '+str(x)+' = '+str(x*x))
elif y==6:
    e=int(input('Which number do you want to put the power to? 1. '+str(a)+' or 2. '+str(x)+'? (Remember only put 1 or 2) '))
    if e==1:
        print(str(a)+' to the power of '+str(x)+' = '+str(a**x))
    elif e==2:
        print(str(x)+' to the power of '+str(a)+' = '+str(x**a))
elif y==7:
    f=int(input('Which number do you want to square root? 1. '+str(a)+' or 2. '+str(x)+' or 3. Both or 4. Pick random? (Remember only put 1, 2, 3 or 4) '))
    if f==1:
        print('The square root of '+str(a)+' is '+sqrt(a))
    elif f==2:
        print('The square root of '+str(x)+' is '+sqrt(x))
    elif f==3:
        print('The square root of '+str(a)+' is '+sqrt(a)+' and the square root of '+str(x)+' is '+sqrt(x))
    elif f==4:
        print('Let me see! I pick...')
        g=random.randint(1,3)
        if g==1:
            print('The square root of '+str(a)+' is '+sqrt(a))
        elif g==2:
            print('The square root of '+str(x)+' is '+sqrt(x))
        elif g==3:
            print('The square root of '+str(a)+' is '+sqrt(a)+' and the square root of '+str(x)+' is '+sqrt(x))
elif y==8:
    print('Bye!!!')
elif y==69:
    print('Very mature!')
else:
    print('No command selected. Self destruction in T-10 seconds. 10... 9... 8... 7... 6... 5... 4... 3... 2... 1... 0... BOOM!')
    exit()

这给了我第26行的问题,上面写着+str(a)。“')导致上述错误。请帮忙。我已经看过了,但它没有告诉我扫描错误的原因。

假设您在此发布的代码与您正在运行的代码相同,实际问题在前面的第13行:

    8. Nothing but quit!\n\'
您永远不会关闭
菜单
字符串,因为
\'
不是字符串结束引号,而是字符串中的文字字符

事实上,当我运行这个时,我得到:

  File "calc.py", line 13
    8. Nothing but quit!\n\'
                           ^
SyntaxError: EOL while scanning string literal
如果我解决了这个问题(通过删除多余的反斜杠),一切都会正常运行,包括除法情况,这就是第26行出现的地方

因此,如果这不是您的实际错误,那么您显然已经修复了真正的问题,并在粘贴代码的过程中添加了一个新问题

正如Jakob Bowyer所指出的,SO语法高亮显示程序实际上发现了同样的问题,例如,请注意第14行(
y=int(输入(菜单))
)作为字符串文字的一部分而不是代码高亮显示。如果你自己使用一个像样的编辑器,If也会做类似的事情


这就是为什么您总是希望使用真正的多行字符串,而不是使用反斜杠继续冒充它们的原因之一。(另一个原因是,在某个时刻,你会在反斜杠后面加一个空格,这会破坏你的代码,尽管你完全看不见。还有一个事实是,一些语法高亮显示者和所有人都会被字符串中的反斜杠连续体搞糊涂……

请复制/粘贴你的错误字符串。或者至少输入错误的名称并标记哪一行是26…实际上,似乎标记语法highlighter显示了错误,我想我刚刚看到了它,它在哪里表示+str(a)+。“')给出了上面的错误。您确实应该使用
'
语法执行多行字符串,而不是使用反斜杠连续体。这避免了混淆人类和自动化工具,如SO的标记…