在python 3 elif错误
我开始蟒蛇几天back..now学习使用'如果'和'elif'...创建一个基本的程序....当我使用elif,它显示我在python 3 elif错误
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
谁能帮我解决这个问题?
对于python,你需要在你的if,elif和其他相同的缩进内拥有一切。将其更改为
if options == 'Add':
print(num1+num2)
print('num1+num2', num1+num2)
elif options == 'Sub':
非常感谢您为UR种类和快速响应。 ..我已经完成了...... :) – user8960966
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")
非常感谢UR的种类和快速响应....我已经完成了.... :) – user8960966
把“打印”的“如果”语句中,就是这样。 它对我有用:
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
试过还有'添加'选项。
非常感谢UR的快速反应.. ..I GOT IT DONE .... :) – user8960966
脚本中的问题是压痕。基本上,为了解决这个问题,你应该改变你的代码是这样的:
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))
在你的代码中,行print('num1+num2=', num1+num2)
结束if
语句,因此elif
没有任何意义
非常感谢你对UR的种类和快速反应....我完成了它.... :) – user8960966
很高兴为您效劳! – Giordano
' print('num1 + num2 =',num1 + num2)'行应该按照与其上面的行相同的方式缩进 –
你可能用'spaces'缩进一行而用另一行缩进'tab'。更改为'选项卡'或两个'空间' –
非常感谢您为UR种类和快速响应....我完成了它.... :) – user8960966