2016-04-23 123 views
-4

当我输入的分数大于1.0时,最后的else语句不会执行,并且不会打印出“超出范围”的行。 的代码如下:最后一条语句没有执行

score = float(raw_input("Enter Score: ")) 

if score>= 0.0: 

    if score <= 1.0: 
     if score >= 0.9: 
      print 'A' 
     elif score >= 0.8: 
      print 'B' 
     elif score >= 0.7: 
      print 'C' 
     elif score >= 0.6: 
      print 'D' 
     else: 
      print 'F' 
else: 
    print 'Value out of range' 
+1

仔细检查你的缩进,这在Python中很重要...或者,简化逻辑 - 如果0.0 <=得分<= 1.0,则使第一行为': – jonrsharpe

+0

将最后两行缩进一级。 – 2016-04-23 08:50:22

+0

是的,谢谢。它的工作现在。 – Bakhtawar

回答

1
if score >= 0.0 and score <= 1.0: 
    if score >= 0.9: 
     print 'A' 
    elif score >= 0.8: 
     print 'B' 
    elif score >= 0.7: 
     print 'C' 
    elif score >= 0.6: 
     print 'D' 
    else: 
     print 'F' 
else: 
    print 'Value out of range' 

或者,

if score>= 0.0: 
    if score <= 1.0: 
     if score >= 0.9: 
      print 'A' 
     elif score >= 0.8: 
      print 'B' 
     elif score >= 0.7: 
      print 'C' 
     elif score >= 0.6: 
      print 'D' 
     else: 
      print 'F' 
    else: 
     print 'Value out of range' 
+0

谢谢......它现在的工作 – Bakhtawar

+0

@downvoters一些意见可以让我提高我的答案。 – AKS

-1

因为score = 1.0 >= 0.0 == True

尝试,而不是:

if score >= 0.0 and score < 1.0: 
0

这是因为你还有属于“分数> = 0.0”而不是“分数< = 1.0”条款。 这是由python中的缩进指示的。

0

您已将“else”语句设置为错误“if” 再次查看代码。

在python缩进块是由空格,所以你必须要小心它。

如果妳有与使用bracets其他编程语言这就像当你把里面的代码“{}” insted的外,在语言的经验,压痕