试着去做。我试图实现一个评分分数,但继续得到除了打印作为输出 score=input('输入分数:') 尝试: 如果得分>=0.9且得分=0.8且得分=0.7且得分=0.6且得分=0.9(假设用户键入1)。Python无法识别“1”>=0.9,因此会出错,并转到except子句
请尝试使用内置函数向我们显示代码片段。否则,我们真的帮不了你。试着去做。我试图实现一个评分分数,但继续得到除了打印作为输出 score=input('输入分数:') 尝试: 如果得分>=0.9且得分=0.8且得分=0.7且得分=0.6且得分=0.9(假设用户键入1)。Python无法识别“1”>=0.9,因此会出错,并转到except子句,python,try-catch,except,Python,Try Catch,Except,请尝试使用内置函数向我们显示代码片段。否则,我们真的帮不了你。 我认为代码的问题在于在同一层上有多个else语句。在同一层上,每个if语句只能有一个else语句。假设您使用的是Python 3,input()返回的数据类型为String,因此当您询问score>=0.9时,您的问题是“1”>=0.9(假设用户键入1)。Python无法识别“1”>=0.9,因此会出错,并转到except子句 您需要使用一种称为“强制转换”的技术,将用户键入的数据类型更改为float或integer。例如float
我认为代码的问题在于在同一层上有多个else语句。在同一层上,每个if语句只能有一个else语句。假设您使用的是Python 3,
input()
返回的数据类型为String
,因此当您询问score>=0.9
时,您的问题是“1”>=0.9
(假设用户键入1
)。Python无法识别“1”>=0.9,因此会出错,并转到except子句
您需要使用一种称为“强制转换”的技术,将用户键入的数据类型更改为float
或integer
。例如float(“0.95”)
返回0.95
,可与0.9
进行比较
另外,您可以在键盘上的选项卡上方或1
旁边键入带反勾的代码行。否则,您可以突出显示所有代码,然后单击堆栈溢出编辑器中的“code”,这会将其转换为其他人易于阅读的代码。尝试:
try:
score = float(input('Enter score:'))
if score >= 0.9 and score <= 1.0:
print("Grade is A")
elif score >= 0.8 and score < 0.9:
print("Grade is B")
elif score >= 0.7 and score < 0.8:
print("Grade is C")
elif score >=0.6 and score < 0.7:
print("Grade is D")
else:
print("Grade is F")
except:
print('bad score')
分数=浮动(输入('输入分数:'))
如果得分>=0.9且得分=0.8且得分<0.9:
印刷品(“B级”)
elif得分>=0.7且得分<0.8:
打印(“C级”)
elif得分>=0.6且得分<0.7:
打印(“D级”)
其他:
打印(“等级为F”)
除:
打印(‘坏分数’)
请以正确的格式提供代码。下面是一个类似的代码,可以使用
问题在于,所以您需要使用强制转换来更改数据类型
score = float(input('Enter score:') )
try:
if score >= 0.9 and score < 1.0:
print("Grade is A")
else:
if score >= 0.8 and score < 0.9:
print("Grade is B")
else:
if score >= 0.7 and score < 0.8:
print("Grade is C")
else:
if score >=0.6 and score < 0.7:
print("Grade is D")
else:
if score < 0.6 and score <=0.0:
print("Grade is F")
except:
print('bad score')
score=float(输入('Enter score:'))
尝试:
如果得分>=0.9且得分<1.0:
打印(“成绩为A”)
其他:
如果得分>=0.8且得分<0.9:
印刷品(“B级”)
其他:
如果得分>=0.7且得分<0.8:
打印(“C级”)
其他:
如果得分>=0.6且得分<0.7:
打印(“D级”)
其他:
如果分数<0.6,并且分数是否可以正确格式化?Python 2或Python 3?如果您使用的是Python 2,则输入函数可能会抛出错误。我使用的是Python 3.7版。我尝试过这种方法,这种方法的问题是,它将为分数介于0.1和1.0之间的人提供分数,但如果我输入“text”作为输入,它不会给我一个except错误。事实上,它显示了一个语法错误ValueError:无法将字符串转换为float:“test”
,然后请将“score=float(input('Enter score:'))”放在try块内。如果我们输入文本,它将转到except块并作为坏分数打印。我已经根据您的评论修改了上述代码。