千家信息网

怎么用Python写剪刀石头布游戏

发表于:2024-10-15 作者:千家信息网编辑
千家信息网最后更新 2024年10月15日,本篇内容主要讲解"怎么用Python写剪刀石头布游戏",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用Python写剪刀石头布游戏"吧!1、电脑赢的情况
千家信息网最后更新 2024年10月15日怎么用Python写剪刀石头布游戏

本篇内容主要讲解"怎么用Python写剪刀石头布游戏",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么用Python写剪刀石头布游戏"吧!

1、电脑赢的情况

电脑(computer)玩家(player)
石头 (stone)剪刀(scissors)
剪刀 (scissor)布(cloth)
布 (cloth)石头(stone)

2、玩家赢的情况

玩家 (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写剪刀石头布游戏"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0