等级输入程序python if else elif

时间:2015-06-24 05:38:49

标签: python

我正在尝试解决这个等级的输入程序。我需要输入0.6到1.0之间的等级并为它们分配一个字母值。我只能使用if else方法,因为我们还没有在课堂上使用其他方法......

score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
        print "Your grade is an F"
elif score >= 0.6:
        print "grade d"
elif score >= 0.7:
        print "grade c"
elif score >= 0.8:
        print "grade b"
elif score >= 0.9:
        print "grade a" 
else:
        print "wrong score"`

4 个答案:

答案 0 :(得分:3)

您不必从最高到最高得分,您也可以这样做:

score = float(raw_input("Enter Score Grade:"))
if score < 0.60:
    print "Your grade is an F"
elif score < 0.7:
    print "grade d"
elif score < 0.8:
    print "grade c"
elif score < 0.9:
    print "grade b" 
elif score <= 1.0:
    print "grade a"
else:
    print "wrong score"

如果您决定从最高到最低进行检查,那么保持一致是一种很好的做法。你最后可以查看你的成绩不及:

score = float(raw_input("Enter Score Grade:"))
if score > 1:
    print "wrong score"
elif score >= 0.9:
    print "grade a"
elif score >= 0.8:
    print "grade b"
elif score >= 0.7:
    print "grade c"
elif score >= 0.6:
    print "grade d"
else:
    print "Your grade is an F"

作为可重复使用的功能:

def grade(score):
    if score > 1:
        return "wrong score"
    if score >= 0.9:
        return "grade a"
    if score >= 0.8:
        return "grade b"
    if score >= 0.7:
        return "grade c"
    if score >= 0.6:
        return "grade d"
    return "Your grade is an F"

score = float(raw_input("Enter Score Grade:"))
print grade(score)

答案 1 :(得分:1)

你应该从最高等级开始: 正如您所见0.92 > 0.60.92 > 0.9 但根据你的逻辑,它将满足第一个if,永远不会达到最后if。 做这样的事情:

score = raw_input("Enter Score Grade:")
sco = int(float(score))
if score < 0.60:
    print ("Your grade is an F")
elif score >= 0.9:
    print ("grade a")
elif score >= 0.8:
    print ("grade b")
elif score >= 0.7:
    print ("grade c")
elif score >= 0.6:
    print ("grade d") 

else:
    print ("wrong score")

答案 2 :(得分:0)

你需要从高年级到低年级。您需要将sco = int(float(score))更改为score = float(score)。在您比较int

时,无需将其更改为float
score = raw_input('Enter the Score')
score = float(score)

if score >= 0.9:
    grade = 'A'
elif score >= 0.8:
    grade = 'B'
elif score >= 0.7:
    grade = 'C'
elif score >= 0.6:
    grade = 'D'
else:
    grade = 'F'
print 'Your Grade : ' + grade  

答案 3 :(得分:0)

你必须不采取第一个条件。如果第一个条件等于0.9,它将等于假,并给出等级'B'。此外,将输入转换为int first

是明智的
score = int(raw_input('Enter the Score'))
score = float(score)

if score > 0.9:
    grade = 'A'
elif score >= 0.8:
    grade = 'B'
elif score >= 0.7:
    grade = 'C'
elif score >= 0.6:
    grade = 'D'
else:
    grade = 'F'
print 'Your Grade : ' + grade 

因此建议使用大于或等于以便处理等于上限值(即0.9)时的情况。

score = int(raw_input('Enter the Score'))
score = float(score)

if score >= 0.9:
    grade = 'A'
elif score >= 0.8:
    grade = 'B'
elif score >= 0.7:
    grade = 'C'
elif score >= 0.6:
    grade = 'D'
else:
    grade = 'F'
print 'Your Grade : ' + grade