【问题标题】:error : 'int' object does not support item assignment [closed]错误:'int'对象不支持项目分配[关闭]
【发布时间】: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(以及liststr 等)作为变量是不明智的。
  • 感谢您的建议,我会记住这一点

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


【解决方案1】:

您已将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 

【讨论】:

    猜你喜欢
    • 2015-03-09
    • 2014-02-18
    • 2012-01-22
    • 2021-10-29
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多