剪刀、石头、布又称“猜丁壳”,是一个猜拳游戏。
游戏规则中,石头克剪刀,剪刀克布,布克石头,相同则平局
思路整理:
1,建立一个列表存储剪刀,石头,布;
2,对输入的数据进行判断,必须要符合要求;
3,从列表中取出选择的项;
4,电脑随机产生一个选项
5,对结果进行判断
import random
select = ['剪刀', '石头', '布']
print('剪刀石头布游戏。')
s = 0
while True:
while True: # 对输入的结果进行筛选,符合要求则退出循环
try:
s = int(input('剪刀-0,石头-1,布-2,请做出的你的选择:'))
except ValueError:
print('输入的数据有误,请重新输入')
s = 10
if 0 <= s <= 2:
break
self_s = select[s] # 提取选项
computer_s = random.choice(select) # 电脑随机产生一个选项
if self_s == computer_s: # 对结果进行判断并输出
print(self_s, '对', computer_s, '---平局')
elif self_s == '剪刀' and computer_s == '石头':
print(self_s, '对', computer_s, '---输了')
elif self_s == '剪刀' and computer_s == '布':
print(self_s, '对', computer_s, '---赢了')
elif self_s == '石头' and computer_s == '布':
print(self_s, '对', computer_s, '---输了')
elif self_s == '石头' and computer_s == '剪刀':
print(self_s, '对', computer_s, '---赢了')
elif self_s == '布' and computer_s == '剪刀':
print(self_s, '对', computer_s, '---输了')
else:
print(self_s, '对', computer_s, '---赢了')