簡體   English   中英

SyntaxError:掃描字符串文字Python計算器時的EOL

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

這是我在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)。 ')'導致上述錯誤。 請幫忙。 我查看了http://code.google.com/hosting/search?q=label%3aPython ,它沒有告訴我掃描錯誤的原因。

假設您在此處發布的代碼與您正在運行的代碼相同,則實際問題在第13行更早:

    8. Nothing but quit!\n\'

你永遠不會關閉menu字符串,因為\\'不是字符串結束引號,它是字符串中的文字字符。

事實上,當我運行這個時,我得到:

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

如果我解決了這個問題(通過刪除多余的反斜杠),一切都運行良好 - 包括分區情況,這是第26行出現的地方。

所以,如果這不是你的實際錯誤,你顯然已經修復了真正的問題,並在粘貼你的代碼的過程中添加了一個新問題...

正如Jakob Bowyer指出的那樣,SO語法高亮顯示實際上發現了同樣的問題 - 例如,注意第14行( y=int(input(menu)) )被突出顯示為字符串文字的一部分,而不是代碼。 如果你自己使用一個體面的編輯器,如果你會做類似的事情。

這是你總是想要使用真正的多行字符串的一個原因,而不是用反斜杠連續偽造它們。 (另一個原因是,在某些時候,你會在反斜杠之后放一個空格,這將破壞你的代碼,盡管你完全看不到。然后有一些語法熒光筆和所有人類因反斜杠延續而混淆的事實在字符串...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM