2017-11-18 2 views
-1

저는 며칠 전에 python을 시작했습니다. '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'로 들여 쓰고 다른 행을'tab'으로 들여 쓰기했을 수 있습니다. '탭'또는 두 공간 모두로 변경하십시오 –

+0

고맙습니다. 빠른 응답을 위해 많이 ... .... 완료되었습니다. :) – user8960966

답변

1

파이썬의 경우, 여러분은 if, elif 그리고 그 밖의 것들을 모두 들여다 볼 필요가 있습니다. 은 "만약"문 내부의 "인쇄"를 넣어

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

고맙습니다. 빠른 대응을 위해 고맙습니다. .. 알아 냈어 .... :) – 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

고맙습니다. .. 알아 냈습니다 .... :) – 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

관련 문제