# 用ython语言实现猜石头剪刀布的游戏的功能
目录
案例:
# 游戏规则:人和电脑分别出剪刀、石头、布,直到人战胜电脑,游戏结束。
代码思路:
①第一步,先把“石头,剪刀,布”放在一个列表里,以便后续使用;
②第二步,通过while循环,来根据人胜的情况结束游戏;
③第三步,通过if判断谁胜谁负,不要忘记手动输入内容不在列表里的情况,并且写好应对措施。
代码:
# 用ython语言实现猜石头剪刀布的游戏的功能
# 游戏规则:人和电脑分别出剪刀、石头、布,直到人战胜电脑,游戏结束。
import random
# 先电脑出,再人出牌 ["电脑","人"]
list_1 = ["石头", "剪刀", "布"]
win_computer = [["石头", "剪刀"], ["布", "石头"], ["剪刀", "布"]]
while True:
computer = random.choice(list_1)
people = input("请输入(石头,剪刀,布):")
if people not in list_1:
print("输入错误!请按要求输入!")
continue
print(f"人出的是:\t\t{people.strip()}")
print(f"电脑出的是:\t{computer}")
if computer == people:
print("平手!比赛继续,人胜可以退出")
continue
elif people in win_computer:
print("电脑胜!比赛继续,人胜可以退出")
continue
else:
print("人胜!比赛结束")
break
结果:
请输入(石头,剪刀,布):h
输入错误!请按要求输入!
请输入(石头,剪刀,布):石头
人出的是:石头
电脑出的是:石头
平手!比赛继续,人胜可以退出
请输入(石头,剪刀,布):石头
人出的是:石头
电脑出的是:布
人胜!比赛结束
案例源代码:
import random
guess_list = ["石头", "剪刀", "布"]
win_combination = [["布", "石头"], ["石头", "剪刀"], ["剪刀", "布"]]
while True:
computer = random.choice(guess_list)
people = input('请输入:石头,剪刀,布\n').strip()
if people not in guess_list:
continue
elif computer == people:
print("平手,再玩一次!")
elif [computer, people] in win_combination:
print("电脑获胜,再玩,人获胜才能退出!")
else:
print("人获胜!")
break
总结:
注:前几天一直写这样的类似案例,特别的顺手,好几天没练了,手就生疏了,一定要多练习,沉得住气,加油,我要相信我自己!