我在我的代码中遇到此语法错误,我无法弄清楚它是否是缩进错误或其他(https://i.stack.imgur.com/Alazd.png)。
# Creating a program that gives us the name,score and grade with variation in score and grade! A
name = input("Enter namez")
# print("name:", name)
print()
score = input("Enter scorez")
# print("score:", score)
score = int(score)
if score >= 75:
grade = 'Excellent'
elif score >= 60:
grade = 'Very good'
elif score >= 50
grade = 'Quite good'
elif score >= 40
grade = 'Not Bad'
elif score >= 30
grade = 'Pretty Bad'
elif score >= 20
grade = 'Horibble'
else: grade = 'Appauling'
print()
print('name:', name, 'score:', score, ' and grade:', grade)
print()
print('So,', name, 'you got a score of', score, 'and hence culminates that your grade becomes', grade, '.')
您在elif条件中错过了数字“:”,在最后四个条件中,将缩进设置为4个空格。
你在semi-colons (:)
条件下缺少elif
。然后缩进在grade
的最后3个任务中也是不正确的。
使用适当的缩进来标记代码块。
你的elif
陈述缺少冒号。
http://www.python.org/dev/peps/pep-0008/#other-recommendations
始终围绕这些二元运算符,两边都有一个空格:赋值(=),扩充赋值(+ =, - =等),比较(==,<,>,!=,<>,<=,> = ,in,not in,is,is not),布尔(和,或者,不是)。
例外情况是使用=
设置命名参数。
也就是说,您可以将代码转换为可重用的功能。
def grade(score):
""" Return a grade based on score."""
rank = ''
if score >= 75:
rank = 'Excellent'
elif score >= 60:
rank = 'Very good'
elif score >= 50:
rank = 'Quite good'
elif score >= 40:
rank = 'Not bad'
elif score >= 30:
rank = 'Pretty bad'
elif score >= 20:
rank = 'Horrible'
else:
rank = 'Appalling'
return rank
name = input('Enter name: ')
score = int(input('Enter score: '))
report_card = {'Name': name, 'Score': score, 'Grade': grade(score)}
print(report_card)
测试运行:
Enter name: X
Enter score: 90
{'Name': 'X', 'Score': 90, 'Grade': 'Excellent'}