Python 剪刀石头布小程序

剪刀、石头、布又称“猜丁壳”,是一个猜拳游戏。

游戏规则中,石头克剪刀,剪刀克布,布克石头,相同则平局

思路整理:

1,建立一个列表存储剪刀,石头,布;

2,对输入的数据进行判断,必须要符合要求;

3,从列表中取出选择的项;

4,电脑随机产生一个选项

5,对结果进行判断

import random

select = ['剪刀', '石头', '布']
print('剪刀石头布游戏。')
s = 0
while True:
    while True:   # 对输入的结果进行筛选,符合要求则退出循环
        try:
            s = int(input('剪刀-0,石头-1,布-2,请做出的你的选择:'))
        except ValueError:
            print('输入的数据有误,请重新输入')
            s = 10
        if 0 <= s <= 2:
            break
    self_s = select[s]  # 提取选项
    computer_s = random.choice(select)  # 电脑随机产生一个选项
    if self_s == computer_s:  # 对结果进行判断并输出
        print(self_s, '对', computer_s, '---平局')
    elif self_s == '剪刀' and computer_s == '石头':
        print(self_s, '对', computer_s, '---输了')
    elif self_s == '剪刀' and computer_s == '布':
        print(self_s, '对', computer_s, '---赢了')
    elif self_s == '石头' and computer_s == '布':
        print(self_s, '对', computer_s, '---输了')
    elif self_s == '石头' and computer_s == '剪刀':
        print(self_s, '对', computer_s, '---赢了')
    elif self_s == '布' and computer_s == '剪刀':
        print(self_s, '对', computer_s, '---输了')
    else:
        print(self_s, '对', computer_s, '---赢了')

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值