从第一个python的脚本到写着这篇博客,日子也不短了,没有像样的写个博客,人老懒散,恶习难改啊,不知老之将至矣,。古人是“发愤忘食,乐以忘忧,才会忘记老之将至”,呜呼,无病呻吟下。。。
好了,言归正传,开始今天的正题,今天这里讲要介绍一个简单的脚本——猜数字,这种小脚本几乎每个编程语言都会有的练习题,好了,下面直接上脚本了:
看脚本之前介绍下这篇博客的3个回合:
1.正常脚本运行
2.添加统计猜测次数的代码
3.如何利用作弊器来作弊,确保百发百中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/usr/bin/env python
import
random
secret
=
random.randint(
1
,
100
)
guess
=
0
tries
=
0
print
"This game is to guess a number for you!"
print
" It is a number form 1 to 99,I'll give you 6 times to try. "
while
guess !
=
secret
and
tries <
6
:
guess
=
input
(
"Please input your guess number: "
)
if
guess < secret:
print
"====Your guess is too low !====\n"
elif
guess > secret:
print
"====Your guess is too high!====\n"
tries
=
tries
+
1
if
guess
=
=
secret:
print
"Congratulations to you! Your guess is right ! "
else
:
print
"No more guesses! Better luck next time for you!"
print
"The secret number was"
,secret
|
上面的脚本十分简单,下面来解释下:
第三行:secret=random.randint(1,100) 表示这个数字是在1--99之间的范围,random这个模块是个求随机数的功能。
第四,五行是定义了2个变量,第6,7行只是提示作用,没有其他作用,相信大家没有问题。
第8行开始while的一个循环,并且规定了次数不能超过6次。
第9--14行是个判断,如果猜测的数字和系统产生的数字比较之后,给出的提示。
第15行的作用,当每输入一次并且猜测的不对的时候,次数加一做记录。
第16行表示猜测正确之后的提示。
下面我来给个脚本运行结果的截图吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
This game
is
to guess a number
for
you!
It
is
a number form
1
to
99
,I'll give you
6
times to
try
.
Please
input
your guess number:
34
=
=
=
=
Your guess
is
too high!
=
=
=
=
Please
input
your guess number:
17
=
=
=
=
Your guess
is
too high!
=
=
=
=
Please
input
your guess number:
10
=
=
=
=
Your guess
is
too high!
=
=
=
=
Please
input
your guess number:
8
=
=
=
=
Your guess
is
too high!
=
=
=
=
Please
input
your guess number:
4
=
=
=
=
Your guess
is
too high!
=
=
=
=
Please
input
your guess number:
2
=
=
=
=
Your guess
is
too high!
=
=
=
=
No more guesses! Better luck
next
time
for
you!
The secret number was
1
|
这个结果看上还不错吧,但是还不是很直观,我想给他加个猜测的次数,这样看起来就更清晰点:我们来看下最后的结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
C:\Documents
and
Settings\qhz\桌面>python a.py
This game
is
to guess a number
for
you!
It
is
a number form
1
to
99
,I'll give you
6
times to
try
.
Please
input
your guess number:
23
=
=
=
=
Your guess
is
too low !
=
=
=
=
Above
is
No
1
Times guess
Please
input
your guess number:
34
=
=
=
=
Your guess
is
too low !
=
=
=
=
Above
is
No
2
Times guess
Please
input
your guess number:
56
=
=
=
=
Your guess
is
too high!
=
=
=
=
Above
is
No
3
Times guess
Please
input
your guess number:
49
=
=
=
=
Your guess
is
too high!
=
=
=
=
Above
is
No
4
Times guess
Please
input
your guess number:
42
=
=
=
=
Your guess
is
too high!
=
=
=
=
Above
is
No
5
Times guess
Please
input
your guess number:
40
=
=
=
=
Your guess
is
too high!
=
=
=
=
Above
is
No
6
Times guess
No more guesses! Better luck
next
time
for
you!
The secret number was
37
|
这个结果就需要在原来的脚本上添加一行代码:
print "Above is No " + str(tries) + " Times guess \n" 打印结果表示这个是第几行。
哈哈,最后这个脚本介绍的差不多了,最后我想给他加个作弊器,想预先知道这个系统产生的数字是多少,你只要 在脚本中添加一行 print secret ,这样一个简单的作弊器就好了,哈哈,你可以运行下,方便你的测试,我们添加这行代码之后,可以运行下看看结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
C:\Documents
and
Settings\qhz\桌面>python a.py
62
The game system generated number
is
62
This game
is
to guess a number
for
you!
It
is
a number form
1
to
99
,I'll give you
6
times to
try
.
Please
input
your guess number:
45
=
=
=
=
Your guess
is
too low !
=
=
=
=
Above
is
No
1
Times guess
Please
input
your guess number:
34
=
=
=
=
Your guess
is
too low !
=
=
=
=
Above
is
No
2
Times guess
Please
input
your guess number:
62
Above
is
No
3
Times guess
Congratulations to you! Your guess
is
right !
|
哈哈,有作弊器就是管用,我也懒得猜了,直接输入结果,GAME OVER......
最后贴下完整的代码:
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
|
#coding=utf-8
#!/usr/bin/env python
'''
Edit By QiaoHaiZhong
Usage:
Version 0.0.1
'''
import
random
secret
=
random.randint(
1
,
100
)
print
secret
print
"The game system generated number is "
+
str
(secret)
+
"\n"
guess
=
0
tries
=
0
print
"This game is to guess a number for you!"
print
" It is a number form 1 to 99,I'll give you 6 times to try. "
while
guess !
=
secret
and
tries <
6
:
guess
=
input
(
"Please input your guess number: "
)
if
guess < secret:
print
"====Your guess is too low !====\n"
elif
guess > secret:
print
"====Your guess is too high!====\n"
tries
=
tries
+
1
print
"Above is No "
+
str
(tries)
+
" Times guess \n"
if
guess
=
=
secret:
print
"Congratulations to you! Your guess is right ! "
else
:
print
"No more guesses! Better luck next time for you!"
print
"The secret number was"
,secret
本文转自你是路人甲还是霍元甲博客51CTO博客,原文链接http://blog.51cto.com/world77/1223169如需转载请自行联系原作者 world77
|