错误:'int'对象不支持项目分配

时间:2018-05-20 06:34:07

标签: python python-3.x for-loop input

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 我不知道如何解决这个问题,请帮助.... 这也是我的第一个问题,如果这个问题有任何错误请告诉我,所以我可以保持正确,不要再犯错误

1 个答案:

答案 0 :(得分:0)

您已使用m同时获得dictmaths分数。您可以更新代码并尝试:

print('welcome to the marklist of the students program')
def student(dict1):
    for name,marks in dict1.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:

 student_name=input('enter student\'s name:')
maths_score=int(input('enter students marks in maths:'))
chemistry_score=int(input('enter student\'s marks in chemistry:'))
physics_score=int(input('enter student\'s marks in physics:'))
cs_score=int(input('enter student\'s marks in computer science:'))
t=maths_score+chemistry_score+physics_score+cs_score
m[student_name]=t
b=input('want to add another students details?? enter yes or no:')
if b=="yes":
   continue
else:
   break

student(m)

并且:使用words来定义变量

输出:

welcome to the marklist of the students program
enter student's name:hari
enter students marks in maths:100
enter student's marks in chemistry:100
enter student's marks in physics:100
enter student's marks in computer science:100
want to add another students details?? enter yes or no:no
student hari has got a total of 400 marks in exam and
 passed the exam with distinction