我无法让脚本返回字母等级。该代码允许我输入成绩,但它不会返回字母等级。它目前没有错误,但无法返回响应。
#Given (1) score, and (2) grade
# For scores between 0.0 - 1.0, this programs prints the letter grade
# For scores enter out of the range of 0.0- 1.0 this program will print an error message
# Use try/catch exception handling to gracefully exit on values outside of the specified range
import sys
score= input ("Enter numeric value of score: ")
score = 0.0-1.0
# Convert input from default string value to integer
floatScore = float (score)
try:
intScore = int (score)
except:
if score > 1.0:
print ("Bad Score")
# Use conditional loop to display letter grade based on user-supplied score
# Print letter grade
elif 1.0 >= score>=.9:
print ("Grade is A" + str(intScore))
elif .9 > score>=.8:
print ("B")
elif .8 >score>=.7:
print ("C")
elif .7 >score>=.6:
print ("D")
elif .6 >score>=.5:
print ("F")
# End program
答案 0 :(得分:1)
只有在except
部分遇到错误时,才会运行脚本的try
部分。将得分转换为int应该没有问题,因此脚本的其余部分永远不会执行。为什么在try-catch块中呢?如果它是0到1之间的数字而不是整数,为什么还要将它转换为int呢?
您还要将分数设置为0.0-1.0,将其重置为-1.0,覆盖用户刚刚输入的内容。这样的事情会更好。
import sys
score= input ("Enter numeric value of score: ")
try:
score = float(score)
if score > 1.0:
print ("Bad Score")
# Use conditional loop to display letter grade based on user-supplied score
# Print letter grade
elif 1.0 >= score>=.9:
print ("Grade is A" + str(score))
elif .9 > score>=.8:
print ("B")
elif .8 >score>=.7:
print ("C")
elif .7 >score>=.6:
print ("D")
elif .6 >score>=.5:
print ("F")
except ValueError:
print("You need to input a number")
# End program