简单的python如果/ else语法不起作用

时间:2017-11-02 12:10:35

标签: python if-statement

我正在尝试创建一个简单的测试记分器,对测试进行评分并为您提供响应 - 但是一个简单的if / else函数未运行 -

Python -

testScore = input("Please enter your test score")


if testScore <= 50:
  print "You didn't pass... sorry!" 
elif testScore >=60 and <=71:
  print "You passed, but you can do better!"

错误是 -

Traceback (most recent call last):
  File "python", line 6
    elif testScore >= 60 and <= 71:
                              ^
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:6)

您在elif声明中错过了testScore

 testScore = input("Please enter your test score")


if testScore <= 50:
  print "You didn't pass... sorry!" 
elif testScore >=60 and testScore<=71:
  print "You passed, but you can do better!"

答案 1 :(得分:2)

下面显示的方法是更好的解决方法,在比较/检查数字时,总是需要将类型转换为整数。

  python中的

input()通常采用字符串

 testScore = input("Please enter your test score")
 if int(testScore) <= 50:
     print("You didn't pass... sorry!" )
 elif int(testScore) >=60 and int(testScore)<=71:
     print("You passed, but you can do better!")