python编制剪刀石头布游戏_Python编辑剪刀石头布游戏

方法一:

# 调用列表(元组)让系统随机出一个值

import random

choice = ['剪刀','石头','布']

computer = random.choice(choice)

player =input('请出拳(剪刀/石头/布): ')

print("我出 %s,电脑出 %s" % (player,computer))

if player =='剪刀':

if computer =='剪刀':

print('\033[31;1m平局\033[0m')

elif computer =='石头':

print('\033[32;1m输了\033[0m')

else:

print('\033[33;1m赢了\033[0m')

elif player =='石头':

if computer =='石头':

print('\033[31;1m平局\033[0m')

elif computer =='布':

print('\033[32;1m输了\033[0m')

else:

print('\033[33;1m赢了\033[0m')

else:

if computer =='剪刀':

print('\033[31;1m输了\033[0m')

elif computer =='石头':

print('\033[32;1m赢了\033[0m')

else:

print('\033[33;1m平局\033[0m')

方法二:(优化)

import random

choice = ['剪刀','石头','布']

computer = random.choice(choice)

player =input('请出拳(剪刀/石头/布): ')

print("我出 %s,电脑出 %s" % (player,computer))

if player == computer:

print('\033[31;1m平局\033[0m')

elif (player =='剪刀' and computer =='布')  \

or (player =='石头' and computer =='剪刀') \

or (player =='布' and computer =='石头'):

print('\033[32;1m赢了\033[0m')

else:

print('\033[33;1m输了\033[0m')

方法三:(优化)

import random

# 将人胜利的情况,提前定义到列表中,人在前,计算机在后

choice = ['剪刀','石头','布']

win_list = [['剪刀,布'],['石头','剪刀'],['布','石头']]

computer = random.choice(choice)

player =input('请出拳(剪刀/石头/布): ')

# %s 预留可变的位置

print("我出 %s,电脑出 %s" % (player,computer))

if player == computer:

print('\033[33;1m平局\033[0m')

# 人机选项的小列表是大列表的一项

elif [player,computer]in win_list:

print('\033[34;2m赢了\033[1m')

else:

print('\033[035;2m输了\033[2m')

方法四:(优化)

import random

# 将人胜利的情况,提前定义到列表中,人在前,计算机在后

choice = ['剪刀','石头','布']

win_list = [['剪刀,布'],['石头','剪刀'],['布','石头']]

prompt ="""(0)剪刀(1)石头(2)布

请选择(0/1/2): """

ind =int(input(prompt))

player = choice[ind]

computer = random.choice(choice)

# %s 预留可变的位置

print("我出 %s,电脑出 %s" % (player,computer))

if player == computer:

print('\033[33;10m平局\033[0m')

# 人机选项的小列表是大列表的一项

elif [player,computer]in win_list:

print('\033[34;20m赢了\033[0m')

else:

print('\033[035;30m输了\033[0m')

方法五:(优化)

import random

choice = ['剪刀','石头','布']

x =0

y =0

z =0

win_list = [['剪刀,布'],['石头','剪刀'],['布','石头']]

prompt ="""(0)剪刀(1)石头(2)布

请选择(0/1/2): """

while x <2 and y <3 and z <2:

ind =int(input(prompt))

player = choice[ind]

computer = random.choice(choice)

print("我出 %s,电脑出 %s" % (player, computer))

if [player,computer]in win_list:

z +=1

elif player == computer:

x +=1

else:

y +=1

if z ==1:

print('\033[33;10m平局\033[0m')

elif x ==1:

print('\033[34;20m赢了\033[0m')

else:

y ==1

print('\033[035;30m输了\033[0m')

if x ==2 or x ==2 or z ==2:

break

方法六:(优化)

import random

# 将人胜利的情况,提前定义到列表中,人在前,计算机在后

choice = ['剪刀','石头','布']

c =0

p =0

win_list = [['剪刀,布'],['石头','剪刀'],['布','石头']]

prompt ="""(0)剪刀(1)石头(2)布

请选择(0/1/2): """

while 1:

ind =int(input(prompt))

player = choice[ind]

computer = random.choice(choice)

print("我出 %s,电脑出 %s" % (player, computer))

if player == computer:

print('\033[33;10m平局\033[0m')

elif [player, computer]in win_list:

c +=1

print('\033[34;20m赢了\033[0m')

else:

p +=1

print('\033[035;30m输了\033[0m')

if p ==2 or c ==2:

break

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值