我正在尝试运行一个简单的程序,但我似乎无法克服这种语法错误。
try:
grade = float(raw_input("Enter a score between 0.0 and 1.0:\n"))
if grade < 0 or grade > 1
print ("That is not between 0.0 or 1.0")
elif grade >= .9
print ("A")
elif grade >= .8 and grade < .9
print ("B")
elif grade >= .7 and grade < .8
print ("C")
elif grade >= .6 and grade < .7
print ("D")
else grade < .6
print ("F")
except:
print ("Enter a numeric value!")
我得到的错误是第3行,如果等级&lt; 0或等级&gt; 1:SyntaxError:语法无效。
答案 0 :(得分:3)
if
,elif
和else
语句(以及其他一些语句)需要在行尾添加冒号。例如:
if grade < 0 or grade > 1:
此外,else
是一个无所不能,因此不使用条件。
else:
答案 1 :(得分:0)
请注意if,else if(elif)和else语句
的语法try:
grade = float(raw_input("Enter a score between 0.0 and 1.0:\n"))
if grade < 0 or grade > 1:
print ("That is not between 0.0 or 1.0")
elif grade >= .9:
print ("A")
elif grade >= .8 and grade < .9:
print ("B")
elif grade >= .7 and grade < .8:
print ("C")
elif grade >= .6 and grade < .7:
print ("D")
else:
print ("F")
except:
print ("Enter a numeric value!")