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
    评论
好的,我们可以使用Python自带的GUI库Tkinter来实现这个剪刀石头布小游戏。 首先,我们需要导入Tkinter库和random库: ```python import tkinter as tk import random ``` 然后,我们需要定义一个函数来处理用户点击按钮的事件,并随机生成电脑的选择: ```python def play_game(player_choice): computer_choice = random.choice(["剪刀", "石头", "布"]) ``` 接下来,我们需要根据用户和电脑的选择来判断胜负,并给出相应的提示: ```python if player_choice == computer_choice: result_label.config(text="平局!") elif player_choice == "剪刀" and computer_choice == "石头": result_label.config(text="你输了!") elif player_choice == "石头" and computer_choice == "布": result_label.config(text="你输了!") elif player_choice == "布" and computer_choice == "剪刀": result_label.config(text="你输了!") else: result_label.config(text="你赢了!") ``` 在主函数中,我们需要创建一个窗口,并添加一些控件,如标签、按钮等: ```python window = tk.Tk() window.title("剪刀石头布游戏") scissors_button = tk.Button(window, text="剪刀", command=lambda: play_game("剪刀")) scissors_button.pack(side="left") rock_button = tk.Button(window, text="石头", command=lambda: play_game("石头")) rock_button.pack(side="left") paper_button = tk.Button(window, text="布", command=lambda: play_game("布")) paper_button.pack(side="left") result_label = tk.Label(window, text="") result_label.pack() window.mainloop() ``` 完整代码如下: ```python import tkinter as tk import random def play_game(player_choice): computer_choice = random.choice(["剪刀", "石头", "布"]) if player_choice == computer_choice: result_label.config(text="平局!") elif player_choice == "剪刀" and computer_choice == "石头": result_label.config(text="你输了!") elif player_choice == "石头" and computer_choice == "布": result_label.config(text="你输了!") elif player_choice == "布" and computer_choice == "剪刀": result_label.config(text="你输了!") else: result_label.config(text="你赢了!") window = tk.Tk() window.title("剪刀石头布游戏") scissors_button = tk.Button(window, text="剪刀", command=lambda: play_game("剪刀")) scissors_button.pack(side="left") rock_button = tk.Button(window, text="石头", command=lambda: play_game("石头")) rock_button.pack(side="left") paper_button = tk.Button(window, text="布", command=lambda: play_game("布")) paper_button.pack(side="left") result_label = tk.Label(window, text="") result_label.pack() window.mainloop() ``` 运行程序,你就可以在窗口中玩剪刀石头布小游戏了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值