输入浮点数,但未到达错误消息

时间:2017-03-03 09:20:45

标签: python

我正在尝试为此作业编写一个程序:

  

编写程序以提示0.0到1.0之间的分数。如果分数超出范围,则输出错误。如果分数介于0.0和1.0之间,请使用下表打印成绩:

Score Grade  
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
     

如果用户输入的值超出范围,请打印一条合适的错误消息并退出。

但它没有打印句子。

try:
    inp = raw_input("Enter Score: ")    

    score = float(inp)

except:
    print "Please enter a score number between 0.0 and 1.0"  
    quit()
if score >= 0.9 :
    print "A" 
elif score >= 0.8 :
    print "B"    
elif score >= 0.7 :
    print "C"    
elif score >= 0.6 :
    print "D" 
elif score < 0.6 :
    print "F"
else:
    print "Your score number is not in the 0 - 1 range."   

7 个答案:

答案 0 :(得分:1)

score = input("Enter Score: ")
scor = float(score)

if 0.0 < scor > 1.0:
    print("error")
elif scor >= 0.9:
    print("A")
elif scor >= 0.8:
    print("B")
elif scor >= 0.7:
    print("C")
elif scor >= 0.6:
    print("D")
elif scor < 0.6:
    print("F")

答案 1 :(得分:0)

就像我在评论部分中突出显示的那样,请对您的if声明进行修改,如下所示:

try:
  inp = raw_input("Enter Score: ")    

  score = float(inp)

except:
   print "Please enter a score number between 0.0 and 1.0"  
   quit()

# change your if statement here:
if score > 1.0 or score < 0.0:
   print "Your score number is not in the 0 - 1 range." 
elif score >= 0.9 :
  print "A" 
elif score >= 0.8 :
   print "B"    
elif score >= 0.7 :
   print "C"    
elif score >= 0.6 :
   print "D" 
elif score < 0.6 :
   print "F"

编辑:首先if分支检查超出范围的score,其值大于1.0且小于0.0,然后输出错误消息。 如果score在限制范围内而不是其他elif块,请检查原始帖子中score的值。

答案 2 :(得分:0)

Pavan,您的代码未通过考试,因为您接受的值高于1.0或小于0.0且不正确。

在某处添加一个检查,确认分数在0.0到1.0之间。

答案 3 :(得分:0)

您应该提出异常以打印它。以下是您要查找的代码更改。

{% for pic in prod_img %}
     <img src="{{ pic.pic_image.url }}" alt="" />
{% endfor %}

希望这有用。

答案 4 :(得分:0)

inp = input("Enter score between 0.0 and 1.0:")
score = float(inp)

if score >= 0.9 and score <= 1.0:
    print("A")
if score >= 0.8 and score < 0.9:
    print("B")
if score >= 0.7 and score < 0.8:
    print("C")
if score >= 0.6 and score < 0.7:
    print("D")
if score >= 0.0 and score <0.6:
    print("F")
elif score > 1.0:
    print("Error, please enter score between 0.0 and 1.0")
    quit()

答案 5 :(得分:0)

scr=(input("Enter the Score: "))
try:
    scr=float(scr)
    if scr>=0.0 and scr<=1.0:
        if scr >= 0.9:
            print("A")
        elif scr >= 0.8:
            print("B")
        elif scr >= 0.7:
            print("C")
        elif scr >= 0.6:
            print("D")
        elif scr<0.6:
            print("F")
    else:
        print("Out Of range")
except:
    print("Try a number")

说明: scr是分数,它将浮点数作为输入,范围是0.0到1.0。如果分数小于0.0或大于1.0,那么它将打印“ Out of Range”。如果输入的输入不是float / int,那么它将打印“ Try a Number”。

答案 6 :(得分:-1)

inp = input("Enter a Score between 0.0 and 1.0")
score = float(inp)

if score >= 0.9 and score <=1.0: 
    print ('A')
elif score >= 0.8 and score <0.9:
    print ('B')
elif score >= 0.7 and score <0.8:
    print ('C')
elif score >= 0.6 and score <0.7:
    print ('D')
elif score >= 0.0 and score <0.6:
    print ('F')
elif score > 1.0 or score < 0.0:
    print ("Error, the score should be in the range between 0.0 and 1.0")
quit()