Python 为什么不是';如果你在分数的参数中加上一个字符串,我就不试着去抓吗? #编写一个程序,提示输入介于0.0和1.0之间的分数。 #如果分数超出范围,则打印错误。 #如果分数介于0.0和1.0之间,请使用下表打印分数: #分数等级>=0.9A>=0.8B>=0.7C>=0.6D=0.8: 等级=“B” elif评分1=0.7: grade=“C” elif评分1=0.6: grade=“D” elif评分10: grade=“F” 其他: grade=“错误:您没有输入数字或输入的数字超出了0.0和1.0的范围!” 印刷品(年级) 除: 打印(“错误:您没有输入数字或输入的数字超出了0.0和1.0的范围!”)

Python 为什么不是';如果你在分数的参数中加上一个字符串,我就不试着去抓吗? #编写一个程序,提示输入介于0.0和1.0之间的分数。 #如果分数超出范围,则打印错误。 #如果分数介于0.0和1.0之间,请使用下表打印分数: #分数等级>=0.9A>=0.8B>=0.7C>=0.6D=0.8: 等级=“B” elif评分1=0.7: grade=“C” elif评分1=0.6: grade=“D” elif评分10: grade=“F” 其他: grade=“错误:您没有输入数字或输入的数字超出了0.0和1.0的范围!” 印刷品(年级) 除: 打印(“错误:您没有输入数字或输入的数字超出了0.0和1.0的范围!”),python,python-3.x,Python,Python 3.x,无论我输入的是0.0和1.0范围之外的整数,还是输入的是该范围内的整数,一切似乎都按预期进行。我创建了一个try-except-catch,如果用户为score输入字符串。但它似乎没有抓住它,我也不明白为什么。我试过这个,效果很好。确保输入是一个不能转换为float的字符串,您应该没事 #Write a program to prompt for a score between 0.0 and 1.0. #If the score is out of range print an error.

无论我输入的是0.0和1.0范围之外的整数,还是输入的是该范围内的整数,一切似乎都按预期进行。我创建了一个try-except-catch,如果用户为score输入字符串。但它似乎没有抓住它,我也不明白为什么。

我试过这个,效果很好。确保输入是一个不能转换为float的字符串,您应该没事

#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

def computegrade(score):
    try:
        score1 = float(score)
        if score1 <= 1.0 and score1 >= 0.9:        
            grade = "A"
        elif score1 < 0.9 and score1 >= 0.8:
            grade = "B"
        elif score1 < 0.8 and score1 >= 0.7:
            grade = "C"
        elif score1 < 0.7 and score1 >= 0.6:
            grade = "D"
        elif score1 < 0.6 and score1 > 0:
            grade = "F"
        else:
            grade = "ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!"
        print(grade) 
    except:
        print("ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!")

那么你怎么称呼它?输出是什么?你可以像这样链接条件:
0.9我无法复制。它对我有用。但是您确实需要更改your except子句来捕获
ValueError
,而不是任何异常。您的字符串是否类似于
“0.8”
?我必须正确输入它。以下是我正在尝试的:>>>>computegrade(龙骑兵)回溯(最近一次调用上次):computegrade(龙骑兵)名称中的文件“”,第1行错误:名称“龙骑兵”未定义>>>computegrade(“龙骑兵”)>>>您尝试了什么?Cary,函数名为computegrade,括号中是我尝试的项。所以在上面的例子中,我用单词dragoon运行函数,然后用引号“dragoon”。对不起,如果这还不清楚的话,我在解释/学习方面还是很新的。对不起,我应该弄明白这一点。计算级(“龙骑士”)的输出是什么?
def computegrade(score):
    try:
        score1 = float(score)
        if score1 <= 1.0 and score1 >= 0.9:
            grade = "A"
        elif score1 < 0.9 and score1 >= 0.8:
            grade = "B"
        elif score1 < 0.8 and score1 >= 0.7:
            grade = "C"
        elif score1 < 0.7 and score1 >= 0.6:
            grade = "D"
        elif score1 < 0.6 and score1 > 0:
            grade = "F"
        else:
            grade = "ERROR: You did not enter a number or you entered a     number out of the range of 0.0 and 1.0!"
        print(grade)
    except:
        print("ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!")
computegrade("fire")
ERROR: You did not enter a number or you entered a number out of the range of 0.0 and 1.0!