2017-11-18 7 views
-1

私はpythonを2,3日後に起動しました。 'if'と 'elif'を使用する方法を学んでいます...基本的なプログラムを作成しました....私がelifを使用すると、それは私に表示されますpythonでelifエラーが発生しました3

私はこれを実行したときに

syntax error:invalid syntax

print('welcome to my calculator') 
num1 = int(input('enter the first number:')) 
num2 = int(input('enter the second number:')) 
print('select options') 
functions = ['Add','Sub','Mul','Div'] 
print (functions) 
options = input("enter the desired option:") 
if options == 'Add': 
    print(num1+num2) 
print('num1+num2=', num1+num2) 
elif options == 'Sub': 
    print(num1-num2) 
print('num1-num2=', (num1-num2)) 

は、私は次のエラー

を得た
elif options == 'Sub': 
    ^
SyntaxError: invalid syntax 

Process finished with exit code 1 

誰も私がこのエラーを解決するのに役立つことはできますか?

+1

' print( 'num1 + num2 ='、num1 + num2) '行は、上の行と同じ方法で字下げしなければなりません。 –

+0

あなたは' spaces'で1行、 'tab'で1行インデントしているかもしれません。 'tab'または' space'の両方に変更してください –

+0

すごくうまくやっちゃいました....私は完了しました.... :) – user8960966

答えて

1

pythonの場合は、if、elif、elseのすべてを同じインデントで使用する必要があります。 「IF」文の内部で「プリント」を入れて

if options == 'Add': 
    print(num1+num2) 
    print('num1+num2', num1+num2) 
elif options == 'Sub': 
+0

UAN KINDとFAST RESPONSEのために大変です。 ..私は完了しました.... :) – user8960966

1
print('welcome to my calculator') 
num1 = int(input('enter the first number:')) 
num2 = int(input('enter the second number:')) 
print('select options') 
functions = ['Add','Sub','Mul','Div'] 
print (functions) 
options = input("enter the desired option:") 
if options == 'Add': 
    print(num1+num2) 
    print('num1+num2=', num1+num2) 
elif options == 'Sub': 
    print(num1-num2) 
    print('num1-num2=', (num1-num2)) 
else:     # you need this line as well 
    print("continue... remaining logic") 
+0

おっしゃっていますので、私たちは種類が豊富で素早い対応をしています....私は完了しました.... :) – user8960966

0

に変更し、それはそれです。 それは私のために働いた:

welcome to my calculator 
enter the first number:3 
enter the second number:2 
select options 
['Add', 'Sub', 'Mul', 'Div'] 
enter the desired option:Sub 
1 
num1-num2= 1 

また、 '追加'オプションを試してみました。

+0

UAN KINDとFAST RESPONSEのために大変です。 ..私は完了しました.... :) – user8960966

1

あなたのスクリプトの問題は念入りです。基本的に、問題を解決するために、あなたはこのようにコードを変更する必要があります、あなたのコードではprint('num1+num2=', num1+num2)if文を終了ラインを

options = input("enter the desired option:") 
if options == 'Add': 
    print(num1+num2) 
    print('num1+num2=', num1+num2) 
elif options == 'Sub': 
    print(num1-num2) 
    print('num1-num2=', (num1-num2)) 

を、そしてのでelifはどんな意味を持っていない

+0

あなたは大丈夫、早い応答のために大丈夫です....私は完了しました.... :) – user8960966

+0

あなたを助けてうれしい! – Giordano

関連する問題