python实践项目四:猜数字游戏

题目要求:在1-20中随机生成一个数字,你来猜,只有6次机会。

 1 #!/usr/bin/python
 2 # -*- coding: UTF-8 -*-
 3 
 4 import random
 5 secretNumber=random.randint(1,20)
 6 print "I'm thinking of a number between 1 and 20."
 7 times = 0
 8 for i in range(1,7):
 9     print "Take a guess:"
10     guess=int(input())
11     if guess<secretNumber:
12         print "Your guess is too low."
13         times+=1
14     elif guess>secretNumber:
15         print "Your guess is too high."
16         times += 1
17     else:
18         times += 1
19         break
20 if guess==secretNumber:
21     print "You are right! The number is:%d,you spend %d times" %(secretNumber,times)
22 else:
23     print "You are wrong,and the number is:%d" %secretNumber

 

运行结果:

python实践项目四:猜数字游戏

 

上一篇:Python 3.5 基本知识1


下一篇:python while循环和for 循环