我正在尝试编写一个程序来获取用户信息并将其添加到列表中,然后我想总计有多少用户输入,但我做不到。我试过运行一个累加器,但我得到 TypeError: unsupported operand type(s) for +: 'int' and 'str'。
def main():
#total = 0
cel_list = []
another_celeb = 'y'
while another_celeb == 'y' or another_celeb == 'Y':
celeb = input('Enter a favorite celebrity: ')
cel_list.append(celeb)
print('Would you like to add another celebrity?')
another_celeb = input('y = yes, done = no: ')
print()
print('These are the celebrities you added to the list:')
for celeb in cel_list:
print(celeb)
#total = total + celeb
#print('The number of celebrities you have added is:', total)
main()
这是没有累加器的输出,但我仍然需要将输入加在一起。我已经注释掉了累加器。
Enter a favorite celebrity: Brad Pitt
Would you like to add another celebrity?
y = yes, done = no: y
Enter a favorite celebrity: Jennifer Anniston
Would you like to add another celebrity?
y = yes, done = no: done
These are the celebrities you added to the list:
Brad Pitt
Jennifer Anniston
>>>
在此先感谢您的任何建议。