- #!/bin/python
- #coding=gb2312
- import random;
- ran=random.randint(1,100);
- con=True;
- count=0;
- while con:
- tem=raw_input("请输入一个你认为的数:");
- t=True;
- while t:
- try:
- guess=int(tem);
- t=False;
- except ValueError:
- tem=raw_input("请重新输入一个整数:");
- if guess==ran:
- count=count+1;
- con=False;
- print "恭喜你,猜中了!你总共猜了%d次!" %count;
- elif guess>ran:
- count=count+1;
- print "你所输入的数偏大!";
- else:
- count=count+1;
- print "你所输入的数偏小!";
- #该片段来自于http://www.codesnippet.cn/detail/110720134554.html
来源: http://www.codesnippet.cn/detail/110720134554.html