猜拳游戏

猜拳游戏

需求:猜拳游戏 finger-guessing game
玩家 (用户自己输入)vs 电脑 (随机出牌)
包含知识点:分支语句,import导入random模块,random.randint(1,4),列表,分支语句if...
 1 # @Author:Mary
 2 # @Time:2020/6/6  12:12
 3 """
 4 需求:猜拳游戏 finger-guessing game
 5 玩家 (用户自己输入)vs 电脑 (随机出牌)
 6 包含知识点:分支语句,import导入random模块,random.randint(1,4),列表,分支语句if...
 7 """
 8 
 9 """
10 #方式一:游戏规则:剪刀石头布,一局定输赢!
11 
12 #1,导入模块random
13 import random
14 
15 #2,定义玩家和电脑的输入结果
16 finger = ["剪刀", "石头", "布"]
17 player = input("玩家请出拳:")
18 computer = random.choice(finger)
19 tip1 = print("电脑出的是:",random.choice(finger))
20 
21 #3,按游戏规则比较列出分支语句,并打印出相应的游戏结果
22 if player == "剪刀" and computer == "石头":
23     print("再接再励!")
24 elif player == "剪刀" and computer == "布":
25     print("洪荒之力")
26 elif player == "石头" and computer == "剪刀":
27     print("洪荒之力")
28 elif player == "石头" and computer == "布":
29     print("再接再励!")
30 elif player == "布" and computer == "剪刀":
31     print("再接再励!")
32 elif player == "布" and computer == "石头":
33     print("洪荒之力")
34 else:
35     print("欧里给!")
36 
37 #方式二:规则:一把定输赢
38 import random
39 finger = ["剪刀", "石头", "布"]
40 cp_list = [["剪刀", "布"], ["布", "石头"], ["石头", "剪刀"]]
41 player = input("玩家请出拳:")
42 computer = random.choice(finger)
43 print("电脑请出拳:", computer)
44 if [player, computer] in cp_list:
45     print("恭喜玩家赢了!这操作,666")
46 elif player == computer:
47     print("平局!欧里给!")
48 else:
49     print("玩家输了!失败是成功之母!")
50 """
51 
52 #方式三:规则:三局两胜,玩家和电脑谁先赢两局,谁胜!
53 
54 import random
55 finger = ["剪刀", "石头", ""]
56 cp_list = [["剪刀", ""], ["", "石头"], ["石头", "剪刀"]]
57 computer = random.choice(finger)   #在列表finger中任意选一个元素
58 a = 0
59 b = 0
60 while True:
61     if a < 2 and b < 2:
62         player = input("玩家请出拳:")
63         if player not in finger:
64             continue
65         print("电脑请出拳:", computer)
66         if [player, computer] in cp_list:
67             print("本局玩家胜!这操作,666")
68             a += 1
69         elif player == computer:
70             print("平局!欧里给!")
71         else:
72             print("本局玩家输了!失败是成功之母!")
73             b += 1
74     elif a == 2:
75         print("恭喜玩家获胜!")
76         break
77     else:
78         print("玩家太逊了,改行学Python吧!")
79         break
80 
81 
82 """
83 练习总结:
84 1,最开始按方式一中写的,代码量太多,可以按方式二中把必赢的组合作为列表放在一个大列表中
85 2,写三局两胜时,最开始把胜败局的次数放在了电脑出拳的后面,以至于没法判断局数,
86    以后要注意,涉及到次数判定,比如密码输入次数,转账失败次数要放在逻辑分支上面;
87 3,方式三中的最后两个分支忘了写break,导致死循环。
88 """
View Code
原文地址:https://www.cnblogs.com/mary2920/p/13058281.html