根据知识点,自己写了一个猜拳小游戏:
import random
def check():
check_input = True
while check_input:
check = int(input('请输入0,1或2。0代表石头,1代表布,2代表剪刀'))
if check not in (0,1,2):
print('由于您不按规则输入,该局已判断为负')
else:
check_input = False
return check
def cai_quan(turn):
v1_count = 0
v2_count = 0
for t in range(turn):
print(f'round{t+1}')
p1 = check()
p2 = random.randint(0,2)
if (p1 - p2) == 1 or (p1 - p2) == -2:
print('Player win!')
v1_count += 1
elif p1 == p2:
print('Draw!')
else:
print('Computer win!')
v2_count += 1
print(f'{turn} games,you win {v1_count} turns, and computer wins {v2_count} turns.')
cai_quan(3)