繁体   English   中英

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 
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”,也没有两个等于或大于等于的等号。 另外,请记住将字符串输入转换为整数。

如果您的python版本3或更高版本,请使用“ input”而不是raw_input。

inp = float(input ('Enter score between 0.0 & 1.0 here: '))

Python知道使用缩进/空格在哪里结束函数。 例如,

if(1 == 1):
    print "I indented here"

以下代码会导致错误,因为Python在if语句中看不到任何内容

 if(1 == 1):
 print "I indented here"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM