python小游戏学习(一) 猜字谜

猜字谜

利用python写出一个简单的猜字谜小游戏,游戏规则如下:
一个人先在心里想出一个数字,由另外一个人去猜,猜的人可以知道说的数比想的那个数字大还是小,有七次机会,次数可以自己设定,其中一个人的想的数用随机函数产生,具体的源代码如下:

import random
a=random.randint(0,20)
count=0

print('hello,I am thinking a number between 1 and 20')
print('please guess what I am thinking')

for count in range(7):
    c=int(input())
    if c==a:
        count=count+1
        break
    elif c>a:
        count=count+1
        print("you guess is higher than my number")
    else:
        count=count+1
        print('you guess is lower than my number')

if c==a:
    print('good job,you have get the right answer in %d times'%count)
else:
    print ('sorry,you have cost all opptiunities')

运行结果如下:
hello,I am thinking a number between 1 and 20
please guess what I am thinking
2
you guess is lower than my number
3
you guess is lower than my number
5
you guess is lower than my number
8
you guess is lower than my number
10
you guess is lower than my number
14
you guess is higher than my number
12
good job,you have get the right answer in 7 times
Press any key to continue …


版权声明:本文为qq_42856481原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。