剪刀石头布游戏思路与总结-2018-02-03

2018-02-03  本文已影响0人  爱吃肉的小码哥
import random
while 1:
    num = random.randint(1,3)
    if num == 1: computer = '剪刀'
    elif num == 2: computer = '石头'
    elif num == 3: computer = '布'
    player = input('开始游戏,请输入指令,输入"end"结束')
    blist = ['剪刀','石头','布']
    if (player not in blist) and (player != 'end'): print('游戏指令输入错误')
    elif (player not in blist) and (player == 'end'):
        print('游戏结束')
        break
    elif player == computer: print('游戏平局')
    elif (player == '剪刀' and computer == '石头') or (player == '石头' and computer == '布') or (player == '布' and computer == '剪刀'):
        print('电脑出了:',computer,'玩家失败')
    elif (player == '剪刀' and computer == '布') or (player == '石头' and computer == '剪刀') or (player == '布' and computer == '石头'):
        print('电脑出了:',computer,'玩家胜利')

大致分三个步骤实现

1.电脑输出的指令

2.玩家输入的指令

3.双方指令进行判断


import random引入Python随机模块

定义一个while无限循环,通常做法是循环条件为1

random.randint()random模块下用来生成整数的随机数,由于此游戏仅有三个触发指令(即'剪刀','石头','布'),所以生成3个随机数.(js自定义随机数公式:Math.floor(Math.random()*(max-min+1)+min))

定义一个list用来表示电脑的输出指令.
判断随机数:为1,2,3时,变量分别为'剪刀','石头','布'.list的作用是检测玩家输入的指令是否匹配

定义一个变量存放玩家输入的指令

判断:

上一篇 下一篇

猜你喜欢

热点阅读