方法一:
# 调用列表(元组)让系统随机出一个值
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
如果觉得《python编制剪刀石头布游戏_Python编辑剪刀石头布游戏》对你有帮助,请点赞、收藏,并留下你的观点哦!