kristin2303 发表于 2020-7-3 09:04:51

新手python代码求助

题目要求是 Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error. If the score is between 0.0 and 1.0, print a grade using the following table:
Score Grade
>= 0.9 A
>= 0.8 B
>= 0.7 C
>= 0.6 D
< 0.6 F
If the user enters a value out of range, print a suitable error message and exit. For the test, enter a score of 0.85.

我写的代码是
score = input("Enter Score: ")
grade = float(score)
if 0.9 <= grade < 1.0:
        print(grade == A)
elif 0.8 <= grade < 0.9:
        print(grade == B)
elif 0.7 <= grade < 0.8:
        print(grade == C)
elif 0.6 <= grade < 0.7:
        print(grade == D)
elif 0.0 <= grade < 0.6:
        print(grade == F)
else:
        print(error)

写完运行的时候总是跳出来代码中的B没有定义,这怎么修改这段代码, 谢谢

Twilight6 发表于 2020-7-3 09:09:46

grade 后面的 字符加上引号才表示字符串

Twilight6 发表于 2020-7-3 09:11:40


代码改成这样:

score = input("Enter Score: ")
grade = float(score)
if 0.9 <= grade < 1.0:
      print(grade,'A')
elif 0.8 <= grade < 0.9:
      print(grade,'B')
elif 0.7 <= grade < 0.8:
      print(grade,'C')
elif 0.6 <= grade < 0.7:
      print(grade,'D')
elif 0.0 <= grade < 0.6:
      print(grade,'F')
else:
      print('error')

kristin2303 发表于 2020-7-3 09:23:19

Twilight6 发表于 2020-7-3 09:11
代码改成这样:

非常感谢{:5_91:}

Twilight6 发表于 2020-7-3 09:24:11

kristin2303 发表于 2020-7-3 09:23
非常感谢

客气了~ 如果问题已经解决,那么就设置【最佳答案】吧
页: [1]
查看完整版本: 新手python代码求助