参考脚本如下,给出一个区间,最大值(top)和最小值(bottom),
然后在这个区间里猜。
【具体脚本】
import random bot=int(input("set range bottom\n")) top=int(input("set range top\n")) rand=random.randint(bot,top) print("random number in["+str(bot)+","+str(top)+"]generated!")#引号里面的内容代表 num=int(input("##Guess the number##\n")) count=1 while(num!=rand): if(num<rand): print("lower than the answer") else: print("higher than the answer") count=count+1 num=int(input("guess again")) print("you get the answer with[%d]times"%count)
【结果】
注意:
1-输出部分
print("random number in["+str(bot)+","+str(top)+"]generated!")
这句话的意思是输出,完整的两个引号的内容,是要输出的
比如“random number in [”以及“,”(一个逗号),包括“]generated ”凡是绿色的字都代表要输出的具体内容
如何把赋予的值带入这个表达式,即在[bottom,top]的区间引用两个变量值时,除了两个变量拼写,
方式是前后都要加上+,是连字符的意思。代表完整输出一句话。
2-区间数值
rand=random.randint(bot,top)
随机数的区间长度,用过random的randint方法来计算具体某个数值,再和下面输入的字符做比较。
while(num!=rand):
3-语法上的缩进问题
在python里面缩进就是语法的一部分,要严格区分,包括条件判断语句中的缩进,代表了哪一个条件从句
严格注意缩进。
以上