我想从用户那里获取输入,并将值传递给(average=dict[x]/6),然后在那里获取resu

2024-09-29 16:33:09 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我为了学习而尝试的一个程序

print("Students_performance_Application") 
x = raw_input("Enter your name : ") 
dict{"Rajesh":"456","Ranjith":"500","Ram":"456"}
average=dict[x]/6
print("Your average is {}%".format(Average))
if Average > 90:
    print(" Hello patron , your Grade is S ")
elif((Average < 90) and (Average >= 60)):
    print(" Hello patron , your Grade is B ")
elif((Average < 60) and (Average > 40)):
    print(" Hello patron , your Grade is C ")
else:
    print(" Hello patron , you have to redo the exams ")

如您所见,我希望从用户那里获得输入,并将值传递给(average=dict[x]/6),然后在那里获得结果。 但即使我传递“Rajesh”或“Ranjith”作为输入值

我得到以下错误:

Traceback (most recent call last):
  File "C:\Python27\results.py", line 5, in <module>
    average=dict[x]/6
TypeError: unsupported operand type(s) for /: 'str' and 'int'

Tags: and程序helloyourisdictgradeprint
1条回答
网友
1楼 · 发布于 2024-09-29 16:33:09

将字典中的分数设置为int object。出现此错误是因为试图用整数(average=dict[x]/6)除字符串对象

print("Students_performance_Application") 
x=raw_input("Enter your name : ") 
dict={"Rajesh":456,"Ranjith":500,"Ram":456}    #  >Update
average=dict[x]/6 
print("Your average is {}%".format(average)) 
if average > 90: 
    print(" Hello patron , your Grade is S ") 
elif((average < 90) and (average >= 60)): 
    print(" Hello patron , your Grade is B ") 
elif((average < 60) and (average > 40)): 
    print(" Hello patron , your Grade is C ") 
else: 
    print(" Hello patron , you have to redo the exams ")

相关问题 更多 >

    热门问题