Python 官方文档:入门教程 => 点击学习
本篇内容主要讲解“怎么用python写剪刀石头布游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python写剪刀石头布游戏”吧!电脑赢的情况电脑(computer)玩家(player)
本篇内容主要讲解“怎么用python写剪刀石头布游戏”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用Python写剪刀石头布游戏”吧!
电脑赢的情况
电脑(computer) | 玩家(player) |
---|---|
石头 (stone) | 剪刀(scissors) |
剪刀 (scissor) | 布(cloth) |
布 (cloth) | 石头(stone) |
玩家赢的情况
玩家 (player) | 电脑(computer) |
---|---|
石头 (stone) | 剪刀(scissors) |
剪刀 (scissor) | 布(cloth) |
布 (cloth) | 石头(stone) |
根据以上规则我们可以用 if 的形式来做到,代码如下:
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
player = input('please enter your choice(stone,scissor,cloth):')
if computer == 'stone':
if player == 'stone':
print('\033[33mdraw\033[0m')
elif player == 'scissor':
print('\033[32myou lose!!!\033[0m')
else:
print('\033[31myou win!!!\033[0m')
if computer == 'scissor':
if player == 'scissor':
print('\033[33mdraw\033[0m')
elif player == 'stone':
print('\033[31myou win!!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
if computer == 'cloth':
if player == 'cloth':
print('\033[33mdraw\033[0m')
elif player == 'scissor':
print('\033[31myou win!!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
print('your choice:%s' % player,'computer choice:%s' % computer )
进阶一
可以把赢的情况写在一个列表中这样可以让上面的脚本更精简些
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]
player = input('please enter your choice(stone,scissor,cloth):')
if computer == player:
print('\033[33mdraw\033[0m')
elif [player,computer] in WinList:
print('\033[33myou win!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
print('your choice:%s' % player,'computer choice:%s' % computer )
进阶二
为了更加方便玩我们分别用数字 0, 1, 2 代替:石头(stone)、剪刀(scissor)、布(cloth)
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]
choice_memu = '''
(0)stone
(1)scissor
(2)cloth
please choice(0/1/2):'''
number = int(input(choice_memu))
player = C_G[number]
if computer == player:
print('\033[33mdraw\033[0m')
elif [player,computer] in WinList:
print('\033[33myou win!!\033[0m')
else:
print('\033[32myou lose!!!\033[0m')
print('your choice:%s' % player,'computer choice:%s' % computer )
进阶三
我们用三局两胜的手段来决出最后的冠军如果是平局就继续猜直至电脑赢了两局或者玩家赢了两局才得出最后的冠军。
import random
C_G = ['stone','scissor','cloth']
computer = random.choice(C_G)
WinList = [['stone','scissor'],['scissor','cloth'],['cloth','stone']]
choice_memu = '''
(0)stone
(1)scissor
(2)cloth
please choice(0/1/2):'''
c_win = 0
p_win = 0
d_win = 1
while c_win < 2 and p_win < 2:
number = int(input(choice_memu))
player = C_G[number]
if computer == player:
d_win+=1
elif [player,computer] in WinList:
p_win+=1
if p_win == 2:
print('\033[31myou win!!!\033[0m')
break
else:
c_win+=1
if c_win == 2:
print('\033[32myou lose!!!\033[0m')
break
total_time = p_win + c_win + d_win
print('you guesse: %s times' % total_time,'you lost: %s times' % c_win,'you win: %s times' % p_win,'draw: %s times' % d_win)
到此,相信大家对“怎么用Python写剪刀石头布游戏”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
--结束END--
本文标题: 怎么用Python写剪刀石头布游戏
本文链接: https://lsjlt.com/news/309560.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0