简体   繁体   English

python中的语法错误; 意外缩进

[英]Syntax error in python; unexpected indent

The aim of this code is to grade the score which is between 0.0 and 1.0. 该代码的目的是对介于0.0和1.0之间的分数进行评分。 below 0.6 is F, >= 0.6 is D, >= 0.7 is C, >= 0.8 is B and >= 0.9 is A; 小于0.6的是F,> = 0.6是D,> = 0.7是C,> = 0.8是B,> = 0.9是A; and I'm getting an error. 我收到一个错误。

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" 

Rewrite your code as above. 如上所述重写代码。 You don't need to have a logical statement for "else" and you don't have 2 equal signs for greater than or equals. 您不需要逻辑地表示“ else”,也没有两个等于或大于等于的等号。 Also, remember to convert your string input to integer. 另外,请记住将字符串输入转换为整数。

Use "input" instead of raw_input if your on python version 3 or higher. 如果您的python版本3或更高版本,请使用“ input”而不是raw_input。

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

Python knows where to end functions using indents/spaces. Python知道使用缩进/空格在哪里结束函数。 For example, 例如,

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

The code below causes an error because Python sees nothing is in the if statement 以下代码会导致错误,因为Python在if语句中看不到任何内容

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

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

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