Python-石头,剪刀,布

要运行这个程序比较简单,只需要熟悉掌握 if 循环便可。代码如下:

import random
choices=['石头','剪刀','布']
computer=random.choice(choices)
player=False
computer_score=0
player_score=0
while True:
    player=input('出石头,或剪刀,或布?')
    #玩家出石头的情况
    if player == '石头':
        print('电脑是',str(computer))
        if computer == '剪刀':
            print('你赢了')
            player_score=player_score+1
        elif computer == '石头':
            print('平局')
        else:
            print('你输了')
            computer_score = computer_score + 1
    #玩家出布的情况
    elif player == '布':
        print('电脑是',str(computer))
        if computer == '剪刀':
            print('你输了')
            computer_score=computer_score+1
        elif computer =='布':
            print('平局')
        else:
            print('你赢了')
            player_score=player_score+1
    #玩家出剪刀的情况
    elif player == '剪刀':
        print('电脑是', str(computer))
        if computer == '石头':
            print('你输了')
            computer_score=computer_score+1
        elif computer =='剪刀':
            print('平局')
        else:
            print('你赢了')
            player_score=player_score+1
    elif player == 'q':
        print('最终结果:')
        print(f'电脑:{computer_score}')
        print(f'玩家:{player_score}')
        break
    else:
        print('请正确选择')
    computer = random.choice(choices)

你可能感兴趣的:(python,python)