python 中的语法错误;意外缩进
Syntax error in python; unexpected indent
此代码的目的是对介于 0.0 和 1.0 之间的分数进行评分。低于 0.6 为 F,>=0.6 为 D,>=0.7 为 C,>=0.8 为 B,>=0.9 为 A;我遇到了错误。
inp = raw_input ('Enter score between 0.0 & 1.0 here: ')
if inp == > 0.9:
print A
elif inp == > 0.8:
print B
elif inp == > 0.7:
print C
elif inp == >0.6:
print D
else inp < 0.6:
print F
Python 知道使用 indents/spaces 在哪里结束函数。
例如,
if(1 == 1):
print "I indented here"
下面的代码会导致错误,因为 Python 发现 if 语句中没有任何内容
if(1 == 1):
print "I indented here"
inp = float(raw_input('Enter score between 0.0 & 1.0 here: ')) ## This line takes the raw input in from command prompt. float('') casts the raw_input string to a float type number.
if inp >= 0.9:
print "A"
elif inp >= 0.8:
print "B"
elif inp >= 0.7:
print "C"
elif inp >=0.6:
print "D"
else:
print "F"
如上重写代码。您不需要 "else" 的逻辑语句,也不需要 2 个等号表示大于或等于。另外,请记住将您的字符串输入转换为整数。
如果您使用 python 版本 3 或更高版本,请使用 "input" 而不是 raw_input。
inp = float(input ('Enter score between 0.0 & 1.0 here: '))
此代码的目的是对介于 0.0 和 1.0 之间的分数进行评分。低于 0.6 为 F,>=0.6 为 D,>=0.7 为 C,>=0.8 为 B,>=0.9 为 A;我遇到了错误。
inp = raw_input ('Enter score between 0.0 & 1.0 here: ')
if inp == > 0.9:
print A
elif inp == > 0.8:
print B
elif inp == > 0.7:
print C
elif inp == >0.6:
print D
else inp < 0.6:
print F
Python 知道使用 indents/spaces 在哪里结束函数。 例如,
if(1 == 1):
print "I indented here"
下面的代码会导致错误,因为 Python 发现 if 语句中没有任何内容
if(1 == 1):
print "I indented here"
inp = float(raw_input('Enter score between 0.0 & 1.0 here: ')) ## This line takes the raw input in from command prompt. float('') casts the raw_input string to a float type number.
if inp >= 0.9:
print "A"
elif inp >= 0.8:
print "B"
elif inp >= 0.7:
print "C"
elif inp >=0.6:
print "D"
else:
print "F"
如上重写代码。您不需要 "else" 的逻辑语句,也不需要 2 个等号表示大于或等于。另外,请记住将您的字符串输入转换为整数。
如果您使用 python 版本 3 或更高版本,请使用 "input" 而不是 raw_input。
inp = float(input ('Enter score between 0.0 & 1.0 here: '))