条件语句会导致python代码中出现语法错误


import traceback
def calculator():

# Get dog age
age = input("Input dog years: ")
try:
# Cast to float
dage = float(age)
if dage > 0:
if dage <= 1:
print ("The given dog age " + str(dage) + " is " + str(round(dage * 15) ) + " in human years") 
elif 1 < dage <= 2:
print ("The given dog age " + str(dage) + " is " + str(round(dage * 12) ) + " in human years")
elif 2 < dage <= 3 :
print ("The given dog age " + str(dage) + " is " + str(round(dage * 9.3) ) + " in human years")
elif 3 < dage <= 4 : 
print ("The given dog age " + str(dage) + " is " + str(round(dage * 8) ) + " in human years")
elif 4 < dage <= 5 : 
print ("The given dog age " + str(dage) + " is " + str(round(dage * 7.2) ) + " in human years")   
elif 5 < dage : 
print ("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0)) + " in human years")
else:
print ("your input should be a positive number")
except:
print(age, "is an invalid age.")
print(traceback.format_exc())

calculator() # This line calls the calculator function

该代码以人类年为单位计算狗的年龄但当它被执行时,第24行出现错误(否则:(

中缺少括号

elif 5 < dage : 
print("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0)) + " in human years")

在打印语句的末尾再添加一个括号

问题的解决方案:您的代码在上一个elifprint语句中缺少str()函数的右括号。以下是正确的陈述:

elif 5 < dage :
print ("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0))) + " in human years")

改进:您还可以使用f字符串来提高可读性。有关更多详细信息,请参阅本教程。此外,您可以简化elif语句中的条件,因为不需要下界。

以下是使用f字符串并进行了一些改进的代码:

def calculator():
age = input('Input dog years: ')
try:
dage = float(age)

if dage > 0:
msg = f'The given dog age {dage} is '
if dage <= 1:
msg += f'{dage * 15:.2f}' 
elif dage <= 2:
msg += f'{dage * 12:.2f}'
elif dage <= 3 :
msg += f'{dage * 9.3:.2f}'
elif dage <= 4: 
msg += f'{dage * 8:.2f}'
elif dage <= 5 : 
msg += f'{dage * 7.2:.2f}'
else: 
msg += f'{36 + 7 * (dage - 5.0):.2f}'
msg +=  ' in human years'
print(msg)
else:
print('your input should be a positive number')
except:
print(f'{age} is an invalid age.')
calculator()

else子句中的最后一个打印缺少一个括号。

这是正确的代码:

import traceback
def calculator():

# Get dog age
age = input("Input dog years: ")
try:
# Cast to float
dage = float(age)
if dage > 0:
if dage <= 1:
print ("The given dog age " + str(dage) + " is " + str(round(dage * 15) ) + " in human years") 
elif 1 < dage <= 2:
print ("The given dog age " + str(dage) + " is " + str(round(dage * 12) ) + " in human years")
elif 2 < dage <= 3 :
print ("The given dog age " + str(dage) + " is " + str(round(dage * 9.3) ) + " in human years")
elif 3 < dage <= 4 : 
print ("The given dog age " + str(dage) + " is " + str(round(dage * 8) ) + " in human years")
elif 4 < dage <= 5 : 
print ("The given dog age " + str(dage) + " is " + str(round(dage * 7.2) ) + " in human years")   
elif 5 < dage : 
print ("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0)) + " in human years"))
else:
print ("your input should be a positive number")
except:
print(age, "is an invalid age.")
print(traceback.format_exc())

calculator() # This line calls the calculator function

最新更新