嗨姐 发表于 2020-11-13 22:07:04

“int“迭代

list1=
list2=
list3=
list4=
num=eval(input("请输入您的学号:"))
while num == (list1):
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum(list2+list3+list4))
    break
while num == (list1):
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum(list2+list3+list4))
    break
while num == (list1):
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum(list2+list3+list4))
    break
while num == (list1):
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum(list2+list3+list4))
    break
else:
    print()

哪里出现了问题???
运行结果是这样的:
请输入您的学号:1
Traceback (most recent call last):
File "C:\Users\86198\AppData\Local\Programs\Python\Python37\p23.py", line 7, in <module>
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum(list2+list3+list4))
TypeError: 'int' object is not iterable
>>>

jackz007 发表于 2020-11-13 22:13:50

本帖最后由 jackz007 于 2020-11-13 22:18 编辑

list1=
list2=
list3=
list4=
num=eval(input("请输入您的学号:"))
if num == list1:
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum((list2 , list3 , list4)))
elif num == list1:
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum((list2 , list3 , list4)))
elif num == list1:
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum((list2 , list3 , list4)))
elif num == list1:
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",sum((list2 , list3 , list4)))
else:
    print()

嗨姐 发表于 2020-11-13 22:24:24

jackz007 发表于 2020-11-13 22:13


想问一下为什么加了括号就不会出现错误了?

jackz007 发表于 2020-11-13 22:29:27

本帖最后由 jackz007 于 2020-11-13 22:31 编辑

嗨姐 发表于 2020-11-13 22:24
想问一下为什么加了括号就不会出现错误了?

   不仅加了括号,而且,还把加号换成了逗号,这样写,sum() 的输入参数就是一个由三个元素组成的元组。

   其实,如果按你的写法,直接用三个元素的和就可以了,没有必须再把这个和作为输入参数调用 sum() 函数。也就是说,直接把 sum() 去掉就可以了。

   就像这样:
    print("该学生成绩为语文",list2,",数学",list3,",英语",list4,",总分",list2+list3+list4)

嗨姐 发表于 2020-11-13 22:35:34

谢谢!!
页: [1]
查看完整版本: “int“迭代