3 案例3:编写石头剪刀布小游戏
3.1 问题
编写game.py脚本,实现以下目标:
计算机随机出拳
玩家自己决定如何出拳
代码尽量简化
- 1
- 2
- 3
3.2 方案
引用random模块生成0-2的随机数,提示并获取用户的整数输入值,应用if扩展语句对随机数与输入值进行对比判断,满足指定条件,输出结果
为简化代码,玩家获胜条件中用and和or两个逻辑运算符进行多个条件内容的判断,用括号来区分运算优先级,所以用户获胜条件为以下3项中任意一项:
1.用户输入剪刀并且随机数是布
2.用户输入石头并且随机数是剪刀
3.用户输入布并且随机数是石头
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:编写脚本
[root@localhost day02]# vim game.py
#!/usr/bin/env python3
import random
#1. 提示并获取用户的输入
player = int(input("请输入 0剪刀 1石头 2布:"))
#2. 让电脑出一个随机数
computer = random.randint(0,2)
#3. 判断用户的输入,然后显示对应的结果
#if 玩家获胜的条件:
if (player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1):
print("赢了,,,,可以去买奶粉了.....")
#elif 玩家平局的条件:
elif player==computer:
print("平局了,,,洗洗手决战到天亮....")
else:
print("输了,,,回家拿钱 再来....")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
或将上面的代码改为以下写法:
引用random模块choice方法随机生成‘石头’、‘剪刀’、‘布’中任意一项,提示并获取用户的输入字符,应用if扩展语句对随机数与输入值进行对比判断,满足指定条件,输出结果问题结果
import random
computer = random.choice(['石头', '剪刀', '布'])
player = input('请出拳(石头/剪刀/布):')
# print('您出了:', player, '计算机出的是:', computer)
print('您出了: %s, 计算机出的是: %s' % (player, computer))
if player == '石头':
if computer == '石头':
print('平局')
elif computer == '剪刀':
print('You WIN!!!')
else:
print('You LOSE!!!')
elif player == '剪刀':
if computer == '石头':
print('You LOSE!!!')
elif computer == '剪刀':
print('平局')
else:
print('You WIN!!!')
else:
if computer == '石头':
print('You WIN!!!')
elif computer == '剪刀':
print('You LOSE!!!')
else:
print('平局')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
步骤二:测试脚本执行
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:1
平局了,,,洗洗手决战到天亮....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:0
赢了,,,,可以去买奶粉了.....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:2
平局了,,,洗洗手决战到天亮....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:1
赢了,,,,可以去买奶粉了.....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:1
输了,,,回家拿钱 再来....
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):石头
您出了: 石头, 计算机出的是: 石头
平局
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):剪刀
您出了: 剪刀, 计算机出的是: 剪刀
平局
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):布
您出了: 布, 计算机出的是: 剪刀
You LOSE!!!
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):石头
您出了: 石头, 计算机出的是: 剪刀
You WIN!!!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
步骤三:改进脚本
执行代码后,在终端显示中,根据提示输入‘石头、剪刀、布’对应数值,通过列表切片获取用户输入字符,引用random模块choice方法电脑随机生成‘石头’、‘剪刀’、‘布’中任意一项字符,将可赢组合放入列表中,如果随机生成电脑值与用户获取字符在可赢列表中,则为可赢组合,输出‘you win’,否则,输出‘you lose’
import random
all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
prompt = '''(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):'''
computer = random.choice(all_choices)
ind = int(input(prompt))
player = all_choices[ind]
print('您出了: %s, 计算机出的是: %s' % (player, computer))
if player == computer:
print('\033[32;1m平局\033[0m')
elif [player, computer] in win_list:
print('\033[31;1mYou WIN!!!\033[0m')
else:
print('\033[31;1mYou LOSE!!!\033[0m')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
测试脚本执行:
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):2
您出了: 布, 计算机出的是: 布
平局
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):1
您出了: 剪刀, 计算机出的是: 剪刀
平局
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):0
您出了: 石头, 计算机出的是: 石头
平局
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):1
您出了: 剪刀, 计算机出的是: 石头
You LOSE!!!
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):2
您出了: 布, 计算机出的是: 剪刀
You LOSE!!!
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):1
您出了: 剪刀, 计算机出的是: 石头
You LOSE!!!
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):0
您出了: 石头, 计算机出的是: 剪刀
You WIN!!!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
4 案例4:完善石头剪刀布小游戏
4.1 问题
编写game2.py脚本,实现以下目标:
基于上节game.py程序
实现循环结构,要求游戏三局两胜
- 1
- 2
4.2 方案
用while循环语句让游戏执行3次,在判断输赢之前用if嵌套方式先判断用户输入的值是否合法,如果合法进行输赢判断,如果不合法重新执行循环语句,三次游戏结束后,即循环结束后,用if语句判断赢了几次,赢得次数大于等于2次,获得最终胜利,否则为输
此程序需要注意的部分在于:
1.要对每次赢局结果进行记录(即赢局次数加1)
2.每局输赢判断之后,游戏次数一定要加1,否则游戏次数将永无休止
4.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:编写脚本
[root@localhost day02]# vim game2.py #!/usr/bin/env python3 import random i = 1 #游戏次数 win = 0 #赢局次数 while i <= 3: #1. 提示并获取用户的输入 player = int(input("请输入 0剪刀 1石头 2布:"))
#2. 让电脑出一个随机数 computer = random.randint(0,2)
#让用户输入合法
if player0 or player1 or player2:
#3. 判断用户的输入,然后显示对应的结果
if (player0 and computer2) or (player1 and computer0) or (player2 and computer1):
print(“第”+str(i)+“局”+“赢了”)
win += 1
elif playercomputer:
print(“第”+str(i)+“局”+“平局”)
else:
print(“第”+str(i)+“局”+“输了”)
i += 1
else:
print(“请重新输入合法数字”)
#4. 判断最终猜拳结果:3局两胜
if win >= 2:
print(“恭喜,你赢了!!”)
else:
print(“你输了!!”)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
步骤二:测试脚本执行
[root@localhost day02]# python3 game2.py
请输入 0剪刀 1石头 2布:3
请重新输入合法数字
请输入 0剪刀 1石头 2布:1
第1局赢了
请输入 0剪刀 1石头 2布:2
第2局赢了
请输入 0剪刀 1石头 2布:3
请重新输入合法数字
请输入 0剪刀 1石头 2布:2
第3局平局
恭喜,你赢了!!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
步骤三:改进脚本
import random
all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
prompt = """(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): """
cwin = 0
pwin = 0
while cwin < 2 and pwin < 2:
computer = random.choice(all_choices)
ind = int(input(prompt))
player = all_choices[ind]
print("Your choice: %s, Computer's choice: %s" % (player, computer))
if player == computer:
print('\033[32;1m平局\033[0m')
elif [player, computer] in win_list:
pwin += 1
print('\033[31;1mYou WIN!!!\033[0m')
else:
cwin += 1
print('\033[31;1mYou LOSE!!!\033[0m')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
测试脚本执行:
[root@localhost day02]# python3 game3.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): 1
Your choice: 剪刀, Computer's choice: 剪刀
平局
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): 2
Your choice: 布, Computer's choice: 石头
You WIN!!!
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): 0
Your choice: 石头, Computer's choice: 剪刀
You WIN!!!
[root@localhost day02]# python3 game3.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): 0
Your choice: 石头, Computer's choice: 布
You LOSE!!!
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2): 1
Your choice: 剪刀, Computer's choice: 石头
You LOSE!!!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-60ecaf1f42.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count">1</span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao" target="_blank">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了404 篇原创文章</span> · <span>获赞 56</span> · <span>访问量 4万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a>
</div>
</div>
</div>
</article>
3 案例3:编写石头剪刀布小游戏
3.1 问题
编写game.py脚本,实现以下目标:
计算机随机出拳
玩家自己决定如何出拳
代码尽量简化
- 1
- 2
- 3
3.2 方案
引用random模块生成0-2的随机数,提示并获取用户的整数输入值,应用if扩展语句对随机数与输入值进行对比判断,满足指定条件,输出结果
为简化代码,玩家获胜条件中用and和or两个逻辑运算符进行多个条件内容的判断,用括号来区分运算优先级,所以用户获胜条件为以下3项中任意一项:
1.用户输入剪刀并且随机数是布
2.用户输入石头并且随机数是剪刀
3.用户输入布并且随机数是石头
3.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:编写脚本
[root@localhost day02]# vim game.py
#!/usr/bin/env python3
import random
#1. 提示并获取用户的输入
player = int(input("请输入 0剪刀 1石头 2布:"))
#2. 让电脑出一个随机数
computer = random.randint(0,2)
#3. 判断用户的输入,然后显示对应的结果
#if 玩家获胜的条件:
if (player==0 and computer==2) or (player==1 and computer==0) or (player==2 and computer==1):
print("赢了,,,,可以去买奶粉了.....")
#elif 玩家平局的条件:
elif player==computer:
print("平局了,,,洗洗手决战到天亮....")
else:
print("输了,,,回家拿钱 再来....")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
或将上面的代码改为以下写法:
引用random模块choice方法随机生成‘石头’、‘剪刀’、‘布’中任意一项,提示并获取用户的输入字符,应用if扩展语句对随机数与输入值进行对比判断,满足指定条件,输出结果问题结果
import random
computer = random.choice(['石头', '剪刀', '布'])
player = input('请出拳(石头/剪刀/布):')
# print('您出了:', player, '计算机出的是:', computer)
print('您出了: %s, 计算机出的是: %s' % (player, computer))
if player == '石头':
if computer == '石头':
print('平局')
elif computer == '剪刀':
print('You WIN!!!')
else:
print('You LOSE!!!')
elif player == '剪刀':
if computer == '石头':
print('You LOSE!!!')
elif computer == '剪刀':
print('平局')
else:
print('You WIN!!!')
else:
if computer == '石头':
print('You WIN!!!')
elif computer == '剪刀':
print('You LOSE!!!')
else:
print('平局')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
步骤二:测试脚本执行
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:1
平局了,,,洗洗手决战到天亮....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:0
赢了,,,,可以去买奶粉了.....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:2
平局了,,,洗洗手决战到天亮....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:1
赢了,,,,可以去买奶粉了.....
[root@localhost day02]# python3 game.py
请输入 0剪刀 1石头 2布:1
输了,,,回家拿钱 再来....
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):石头
您出了: 石头, 计算机出的是: 石头
平局
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):剪刀
您出了: 剪刀, 计算机出的是: 剪刀
平局
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):布
您出了: 布, 计算机出的是: 剪刀
You LOSE!!!
[root@localhost day02]# python3 game.py
请出拳(石头/剪刀/布):石头
您出了: 石头, 计算机出的是: 剪刀
You WIN!!!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
步骤三:改进脚本
执行代码后,在终端显示中,根据提示输入‘石头、剪刀、布’对应数值,通过列表切片获取用户输入字符,引用random模块choice方法电脑随机生成‘石头’、‘剪刀’、‘布’中任意一项字符,将可赢组合放入列表中,如果随机生成电脑值与用户获取字符在可赢列表中,则为可赢组合,输出‘you win’,否则,输出‘you lose’
import random
all_choices = ['石头', '剪刀', '布']
win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']]
prompt = '''(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):'''
computer = random.choice(all_choices)
ind = int(input(prompt))
player = all_choices[ind]
print('您出了: %s, 计算机出的是: %s' % (player, computer))
if player == computer:
print('\033[32;1m平局\033[0m')
elif [player, computer] in win_list:
print('\033[31;1mYou WIN!!!\033[0m')
else:
print('\033[31;1mYou LOSE!!!\033[0m')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
测试脚本执行:
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):2
您出了: 布, 计算机出的是: 布
平局
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):1
您出了: 剪刀, 计算机出的是: 剪刀
平局
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):0
您出了: 石头, 计算机出的是: 石头
平局
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):1
您出了: 剪刀, 计算机出的是: 石头
You LOSE!!!
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):2
您出了: 布, 计算机出的是: 剪刀
You LOSE!!!
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):1
您出了: 剪刀, 计算机出的是: 石头
You LOSE!!!
[root@localhost day02]# python3 game2.py
(0) 石头
(1) 剪刀
(2) 布
请选择(0/1/2):0
您出了: 石头, 计算机出的是: 剪刀
You WIN!!!
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
4 案例4:完善石头剪刀布小游戏
4.1 问题
编写game2.py脚本,实现以下目标:
基于上节game.py程序
实现循环结构,要求游戏三局两胜
- 1
- 2
4.2 方案
用while循环语句让游戏执行3次,在判断输赢之前用if嵌套方式先判断用户输入的值是否合法,如果合法进行输赢判断,如果不合法重新执行循环语句,三次游戏结束后,即循环结束后,用if语句判断赢了几次,赢得次数大于等于2次,获得最终胜利,否则为输
此程序需要注意的部分在于:
1.要对每次赢局结果进行记录(即赢局次数加1)
2.每局输赢判断之后,游戏次数一定要加1,否则游戏次数将永无休止
4.3 步骤
实现此案例需要按照如下步骤进行。
步骤一:编写脚本
[root@localhost day02]# vim game2.py #!/usr/bin/env python3 import random i = 1 #游戏次数 win = 0 #赢局次数 while i <= 3: #1. 提示并获取用户的输入 player = int(input("请输入 0剪刀 1石头 2布:"))
#2. 让电脑出一个随机数 computer = random.randint(0,2)