简体   繁体   English

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

[英]SyntaxError: EOL while scanning string literal Python Calculator

This is my Calculator in Python 3.3.0 This is my program... 这是我在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()

This is giving me problems on the 26th line where it says +str(a). 这给了我第26行的问题,它说的是+ str(a)。 The ')' is causing the error above. ')'导致上述错误。 Please help. 请帮忙。 I have looked at http://code.google.com/hosting/search?q=label%3aPython and it doesn't give me information on why it is scanning wrong. 我查看了http://code.google.com/hosting/search?q=label%3aPython ,它没有告诉我扫描错误的原因。

Assuming that the code you posted here is the same code you're running, the actual problem is earlier, at line 13: 假设您在此处发布的代码与您正在运行的代码相同,则实际问题在第13行更早:

    8. Nothing but quit!\n\'

You never close the menu string, because \\' is not a string-closing quote, it's a literal character inside the string. 你永远不会关闭menu字符串,因为\\'不是字符串结束引号,它是字符串中的文字字符。

And in fact, when I run this, I get: 事实上,当我运行这个时,我得到:

  File "calc.py", line 13
    8. Nothing but quit!\n\'
                           ^
SyntaxError: EOL while scanning string literal

And if I fix that (by removing the excess backslash), everything runs fine—including the division case, which is where line 26 comes up. 如果我解决了这个问题(通过删除多余的反斜杠),一切都运行良好 - 包括分区情况,这是第26行出现的地方。

So, if this isn't your actual error, you've apparently fixed the real problem and added a new one in the process of pasting your code here… 所以,如果这不是你的实际错误,你显然已经修复了真正的问题,并在粘贴你的代码的过程中添加了一个新问题...

As Jakob Bowyer pointed out, the SO syntax highlighter actually found this same problem—for example, notice that line 14 ( y=int(input(menu)) ) is highlighted as part of a string literal, rather than as code. 正如Jakob Bowyer指出的那样,SO语法高亮显示实际上发现了同样的问题 - 例如,注意第14行( y=int(input(menu)) )被突出显示为字符串文字的一部分,而不是代码。 If you're using a decent editor yourself, if will do something similar. 如果你自己使用一个体面的编辑器,如果你会做类似的事情。

This is one reason you always want to use real multiline strings, rather than faking them with backslash continuation. 这是你总是想要使用真正的多行字符串的一个原因,而不是用反斜杠连续伪造它们。 (Another reason is that at some point, you will put a space after the backslash, which will break your code, despite being completely invisible to you. Then there's the fact that some syntax highlighters, and all human beings, get confused by backslash continuations in strings…) (另一个原因是,在某些时候,你会在反斜杠之后放一个空格,这将破坏你的代码,尽管你完全看不到。然后有一些语法荧光笔和所有人类因反斜杠延续而混淆的事实在字符串...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM