猜拳小游戏编程(python)

import random
d = {0:'石头',1:'剪刀',2:'布'}#创建字典

'''猜拳函数'''
def guess_game(player):
    computer = random.randint(0, 2)#电脑随机产生0-2
    if (player == 0 and computer == 1) or (player == 1 and computer == 2) or (player == 2 and computer == 0):
        print('电脑出的是%s,玩家出的是%s。**玩家赢啦**' % (d[computer], d[player]))
    elif player == computer:
        print('电脑出的是%s,玩家出的是%s,**平局**' % (d[computer], d[player]))
    else:
        print('电脑出的是%s,玩家出的是%s,**玩家输了**' % (d[computer], d[player]))
    print('\n')

while True:
    print('猜拳游戏:0代表石头,1代表剪刀,2代表布。e退出游戏')
    player = input('请出拳:0 ,1,2:\n')
    if (player == 'e' or player == '0' or player == '1' or player == '2' ):
        if player == 'e':
            break
        player = int(player)
        guess_game(player)
    else:
        print('请输入0-2猜拳,或者输入e退出游戏')
        print('\n')

你可能感兴趣的:(python小程序)