python invalid syntax elif_Invalid Elif Syntax Error in Python 3 program

在Python 3程序中,用户遇到了elif语句的语法错误。问题出现在print语句缺少右括号和缩进不正确。答案提供了修正后的代码,确保每个elif语句紧跟在其对应的if语句之后,并且所有print语句都已正确关闭。
摘要由CSDN通过智能技术生成

问题

I keep getting an invalid sytnax error in the elif statement of my code. What am I doing wrong?

# define the functions for each math operation

#

def add (a, b) :

return a + b

def subtract (a, b) :

return a - b

def multiply (a, b) :

return a * b

def divide (a, b) :

return a / b

def remainder (a, b) :

return a % b

def welcome_message ( first_name ) :

print ("Hi ", first_name, " " ". Welcome to Project 3!")

welcome_message("Prof. Shah")

loop = 1

while loop ==1:

print ("Select operation.")

print ("1. Add")

print ("2. Subtract")

print ("3. Multiply")

print ("4. Divide")

print ("5. Remainder")

choice = input("Enter choice :")

num1 = int(input |"Please enter your first number: ")

num2 = int(input |"Please enter your second number: ")

if choice == '1' :

print(num1, "+", num2, "=", add (num1,num2)

elif choice == '2' :

print(num1, "-", num2, "=", subtract (num1,num2)

elif choice == '3' :

print(num1, "*", num2, "=", multiply (num1,num2)

elif choice == '4' :

print(num1, "/", num2, "=", divide (num1,num2)

elif choice == '5' :

print(num1, "%", num2, "=", remainder (num1,num2)

回答1:

if choice == '1' :

print(num1, "+", num2, "=", add(num1,num2))

elif choice == '2' :

print(num1, "-", num2, "=", subtract(num1,num2))

...

your indentation seems to be off, and you were missing a closing brace at the end of every print statement.

回答2:

if choice == '1' :

print(num1, "+", num2, "=", add (num1,num2)

elif choice == '2' :

print(num1, "-", num2, "=", subtract (num1,num2)

elif choice == '3' :

print(num1, "*", num2, "=", multiply (num1,num2)

elif choice == '4' :

print(num1, "/", num2, "=", divide (num1,num2)

elif choice == '5' :

print(num1, "%", num2, "=", remainder (num1,num2)

Problem is in here. if "if" block is going to process, then you have to start with if after then elif. like:

if choice == '1' :

print(num1, "+", num2, "=", add (num1,num2)

elif choice == '2' :

print(num1, "-", num2, "=", subtract (num1,num2)

elif choice == '3' :

print(num1, "*", num2, "=", multiply (num1,num2)

elif choice == '4' :

print(num1, "/", num2, "=", divide (num1,num2)

elif choice == '5' :

print(num1, "%", num2, "=", remainder (num1,num2)

来源:https://stackoverflow.com/questions/27347442/invalid-elif-syntax-error-in-python-3-program

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值