今天探讨了下关于python的一个练习剪刀石头布,可能程序还是多少有些不足,欢迎各位批评指正:
代码如下:
import random
def main():
#将玩家和机器视为两个参量,机器产生随机数1-3,代表着'1':'石头','2':'剪刀','3':'布'
#然后玩家可以输入进行模拟比赛了
print('*******************begin*******************')
#先设置一个空表以及初始化infos
player_info = []
player_score = 0
infos = {'1':'石头','2':'剪刀','3':'布'}
while True:
#控制程序可以一直比赛下去
computer = str(random.randint(1,3))
player = input('请选择:1-石头,2-剪刀,3-布:')
if player not in ['1','2','3']:
print('输入有误,请重新输入:')
continue
else:
ret = (player,computer)
player_info.append(ret)
if ret==('1','2') or ret==('2','3') or ret==('3','1'):
print('玩家赢了')
player_score += 1
elif player == computer:
print('平局')
else:
print('机器赢了')
player_score -= 1
print('对战双方结果:玩家:%s,机器;%s'%(infos[player],infos[computer]))
#设置结束条件
choice = input('\n是否继续游戏?(yes/no):')
<