Python 剪刀石头布游戏代码:简单易懂,随机出拳,胜负判断
import random
print('剪刀石头布游戏开始!')
print('请输入1代表剪刀,2代表石头,3代表布,输入0结束游戏。')
while True:
player = int(input('请出拳:'))
if player == 0:
print('游戏结束,谢谢参与!')
break
elif player not in [1, 2, 3]:
print('请正确输入!')
continue
computer = random.randint(1, 3)
print('玩家出的是:', end='')
if player == 1:
print('剪刀')
elif player == 2:
print('石头')
else:
print('布')
print('电脑出的是:', end='')
if computer == 1:
print('剪刀')
elif computer == 2:
print('石头')
else:
print('布')
if player == computer:
print('平局!')
elif (player == 1 and computer == 3) or \
(player == 2 and computer == 1) or \
(player == 3 and computer == 2):
print('你赢了!')
else:
print('你输了!')
该代码使用 random.randint(1, 3) 函数生成电脑出拳的随机数,分别代表剪刀、石头、布。使用条件判断语句判断胜负,输出对应的结果。
原文地址: https://www.cveoy.top/t/topic/nLqK 著作权归作者所有。请勿转载和采集!