【发布时间】:2018-05-20 06:34:07
【问题描述】:
print('welcome to the marklist of the students program')
def student(dict):
for name,marks in dict.items():
if marks>60:
print(f'student {name} has got a total of {marks} marks in exam and\n passed the exam with distinction ')
elif marks>40:
print(f'student {name} has got a total of {marks} marks in exam and\n passed the exam with first class ')
elif marks>20:
print(f'student {name} has got a total of {marks} marks in exam and\n passed the exam with second class ')
else:
print(f'student {name} has got a total of {marks} marks in exam and\n ha failed his exam ')
m={}
while True:
a=input('enter student\'s name:')
m=int(input('enter students marks in maths:'))
c=int(input('enter student\'s marks in chemistry:'))
p=int(input('enter student\'s marks in physics:'))
cs=int(input('enter student\'s marks in computer science:'))
t=m+c+p+cs
m[a]=t
b=input('want to add another students details?? enter yes or no:')
if b=="yes":
continue
else:
break
student(m)
所以我想制作一个程序来计算学生的分数并显示学生所属的类别,但是......
当我运行这个程序时,它给了我一条错误消息 line 22, in <module>
m[a]=t
TypeError: 'int' object does not support item assignment
我不知道如何解决这个问题请帮助....
这也是我的第一个问题,如果这个问题有任何错误,请告诉我,以便我可以继续纠正,不要再犯错误
【问题讨论】:
-
您有两个名为
m的变量:一个 dict (m={}) 和一个 int (m=int(input(...)))。 -
请不要保存字符 - 如果您使用更长的变量名,您不会自己绊倒。
-
使用名称
dict(以及list、str等)作为变量是不明智的。 -
感谢您的建议,我会记住这一点
标签: python python-3.x for-loop input