0

これは 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()

これにより、+str(a) と書かれている 26 行目で問題が発生しています。「)」が上記のエラーの原因です。助けてください。http://code.google.com/hosting/search?q=label%3aPythonを調べましたが、スキャンが間違っている理由についての情報がありません。

4

1 に答える 1

2

ここに投稿したコードが実行中のコードと同じであると仮定すると、実際の問題は以前の 13 行目にあります。

    8. Nothing but quit!\n\'

は文字列を閉じる引用符ではなく、文字列内のリテラル文字であるmenuため、文字列を閉じることはありません。\'

実際、これを実行すると、次のようになります。

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

これを (余分なバックスラッシュを削除して) 修正すると、26 行目の除算のケースを含め、すべてが正常に実行されます。

したがって、これが実際のエラーでない場合は、コードをここに貼り付ける過程で実際の問題が修正され、新しい問題が追加されたようです…</p>

Jakob Bowyer が指摘したように、SO 構文ハイライターは実際にこれと同じ問題を発見しましたy=int(input(menu))。自分でまともなエディターを使用している場合は、同様のことができます。

これは、バックスラッシュの継続で偽装するのではなく、常に実際の複数行の文字列を使用したい理由の 1 つです。(もう 1 つの理由は、ある時点で、バックスラッシュの後にスペースを入れることです。これにより、完全に見えなくても、コードが壊れます。次に、一部の構文ハイライターとすべての人間が、バックスラッシュの継続によって混乱するという事実があります。文字列で…)

于 2012-12-06T20:50:02.207 に答える