温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-02-22 16:45:18 来源:亿速云 阅读:220 作者:iii 栏目:开发技术

本篇内容主要讲解“怎么用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写剪刀石头布游戏”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI