云亦然
天下皆白,唯我独黑。非攻墨门,兼爱平生。

Python猜拳游戏

2018-12-19 15:32:43 学无止境 python
Word count: 776 | Reading time: 3min

学习使我快乐,在学习了众多的python内容之后,
例如:注释,变量,字符串,if条件语句等等。
好像学了东西没记录下来,就像没有学到东西一样。
然后我就学到了python的一个猜拳游戏,对我来说,是真的有点无从下手的感觉。。。
先上一张游戏图片,有点简陋 哈哈

然后下面就是代码块

[Python猜拳游戏][]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87

# coding = utf-8
# game by yunyiran

import random

def start():
ComputerWinTime = 0
PlayerWinTime = 0
dict1 = {1: "剪刀", 2: "石头", 3: "布"}
print("<欢迎来到云亦然猜拳小游戏>")
print("****************")
print("1.开始新游戏")
print("2.我要走了")
print("输入其它内容即视为退出")
print("****************")
XuanZe = input("请选择:")
if XuanZe == '1':
while ComputerWinTime < 2:
rand = random.randint(1,3)
while True:
onetime = input('请输入手势[剪刀、石头、布]')
onetime = onetime.strip()
if onetime in dict1.values():
break
else:
print("Error:{}是个错误的手势".format(onetime))

if rand == 1:
if onetime == "剪刀":
result = ("和我不相上下")

elif onetime == "石头":
result = ("恭喜!你赢了我。")
PlayerWinTime += 1
else:
result = ("你输给了我哦")
ComputerWinTime += 1
elif rand == 2:
if onetime == "剪刀":
result = ("你输给了我哦")
ComputerWinTime += 1
elif onetime == "石头":
result = ("和我不相上下")
else:
result = ("恭喜!你赢了我。")
PlayerWinTime += 1
else:
if onetime == "剪刀":
result = ("恭喜!你赢了我。")
elif onetime == "石头":
result = ("你输给了我哦")
ComputerWinTime += 1
else:
result = ("平局")
print("电脑出的是:%s\n你出的是:%s\n结果是:%s"%(dict1[rand],onetime,result))
else:
if PlayerWinTime == 2:
print("累计赢了两局,还来吗?")
print("1.继续")
print("2.不来了")
XuanZe = input("请选择:")

if XuanZe == '1':
start()
else:
exit()
else:
print("累计输了两局,还来吗?")
print("1.继续")
print("2.不来了")
XuanZe = input("请选择:")

if XuanZe == '1':
start()
else:
exit()
elif XuanZe == '2':
print("你走吧!")
exit()
else:
exit()
start()




嗯,接下来就说一下那个新手遇到的坑。

(1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)

该错误将发生在类似如下代码中:

[]
1
2
if spam == 42
print('Hello!')

(2)使用 = 而不是 ==(导致“SyntaxError: invalid syntax”)

= 是赋值操作符而 == 是等于比较操作。该错误发生在如下代码中:

[]
1
2
if spam = 42:
print('Hello!')

(3)错误的使用缩进量。(导致“IndentationError:unexpected indent”、“IndentationError:unindent does not match any outer indetation level”以及“IndentationError:expected an indented block”)

记住缩进增加只用在以:结束的语句之后,而之后必须恢复到之前的缩进格式。该错误发生在如下代码中:

[]
1
2
3
4
5
6
7
8
9
10
11
12
13
print('Hello!')
print('Howdy!')

#或者

if spam == 42:
print('Hello!')
print('Howdy!')

#或者

if spam == 42:
print('Hello!')

完。


        

Author: 云亦然

Link: http://JaneBraun.github.io/2018/12/19/Python猜拳游戏/

Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

< PreviousPost
Python的终端影集
NextPost >
入门渗透测试DVWA靶机的搭建
CATALOG