版本I:
1. 生成一个随机数
2. 输入一个数字
3. 比较生成的随机数和输入的数字大小,并给予提示
4. 根据提示再次输入,再次提交,5次机会
5. 输入够5次之后,结束猜测,打印生成随机数答案
-
#!/usr/bin/env python
-
#coding:utf-8
-
-
import random
-
-
number1 = random.randint(1,100)
-
count = 0
-
while 1 and count < 5:
-
number2 = int(raw_input("please input a number: "))
-
if number2 > number1:
-
print "this number is too big,please reinput a number."
-
elif number2 < number1:
-
print "this number is too little,please reinput a number."
-
else:
-
print "great! this number is %d"% number2
-
count +=1
-
-
if count == 5:
-
print "this game is over,the number1 is %d" %number1
版本II:
1. 生成一个随机数
2. 判断是否为数字,是否在1-100之间
3. 根据大小给出提示,再次输入猜测数字
4. 如果5
(0,1,2,3,4)次还没有猜出来,打印正确答案
-
#coding:utf-8
-
-
import random
-
-
number = random.randint(1,101) #生成一个1-100的随机数
-
guess = 0 #猜测次数从0次开始
-
-
while guess < 5: #条件为真且猜测次数不大于5次继续
-
num_input = raw_input("please input one integer that is in 1 to 100: ") #输入一个1-100的整数
-
if not num_input.isdigit(): #判断输入的是否为整数,如果不是提示请输入整数
-
print "please input integer."
-
elif int(num_input) < 0 or int(num_input) >=100: #判断输入的输入的整数是否在1-100之间
-
print "please input integer in 1 to 100"
-
else:
-
tmpNumber = int(num_input)
-
if tmpNumber == number: #判断输入的整数和随机生成的整数是否相等,如果相等,打印出来正确多少次猜中
-
print "you are right! great %d times guess it ~" % guess
-
break
-
elif int(num_input) < number: #输入的整数小于随机生成的整数,给予提示
-
print "%d is less than number." % tmpNumber
-
else:
-
print "%d is more than number." % tmpNumber
-
guess += 1 #猜测次数加1
-
if guess == 5: #如果猜测次数等于5次(0,1,2,3,4)依然没有猜对,打印正确答案
-
print "the number is %d" % number
阅读(1111) | 评论(0) | 转发(0) |