【发布时间】:2020-05-23 17:12:38
【问题描述】:
我正在尝试对其中只有整数的列表求和,但在第 22 行出现错误 TypeError: unsupported operand type(s) for +: 'int' and 'str'。这是一个 '石头剪刀布游戏
import random
import time
score = []
while True:
print('r/p/s')
x=input('You: ')
y=random.randint(1,3)
if str(y) == '1':
print('CPU: r')
elif str(y) == '2':
print('CPU: p')
elif str(y) == '3':
print('CPU: s')
if x == 'r' and str(y) == '3' or x == 'p' and str(y) == '1' or x == 's' and str(y) == '2':
print('WIN')
score.append('1')
print('SCORE:',end='')
print(sum(score))
elif x == 'r' and str(y) == '1' or x == 'p' and str(y) == '2' or x == 's' and str(y) == '3':
print('TIE')
elif x == 'r' and str(y) == '2' or x == 'p' and str(y) == '3' or x == 's' and str(y) == '1':
print('LOSS')
if input('again? (y/n) ') == 'y':
continue
else:
print(' -------- ')
print('THANKS FOR PLAYING!')
print(' -------- ')
time.sleep(2)
break
【问题讨论】: