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)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

多妤转晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值