预期类型int在Pycharm中浮动

时间:2018-08-29 16:51:35

标签: python-3.x floating-point int

    sub1=int(input("Enter the marks of the first subject: "))
    sub2=int(input("Enter the marks of the second subject: "))
    sub3=int(input("Enter the marks of the third subject: "))
    sub4=int(input("Enter the marks of the fourth subject: "))
    sub5=int(input("Enter the marks of the fifth subject: "))
    avg=(sub1+sub2+sub3+sub4+sub5)/5
    if(avg>=90):
      print("Grade A:")
    elif(avg>=80&avg<90):
      print("Grade B:")
    elif(avg>=70&avg<80):
      print("Grade C:")
    elif(avg>=60&avg<70):
      print("Grade D:")
    else:
      print("Grade F:")

1 个答案:

答案 0 :(得分:0)

问题在于&的优先级高于比较运算符(>= / <),因此avg>=80&avg<90被解析为avg>=(80&avg)<90。然后,它尝试在两个值之间执行bitwise &,但失败了。

您最好将二者写成

avg>=70 and avg<80

({and是短路运算符,并且具有较低的优先级),或者

70 <= avg < 80