您的位置:首页 > 其它

猜数字小游戏

2018-03-30 17:42 176 查看
参考脚本如下,给出一个区间,最大值(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里面缩进就是语法的一部分,要严格区分,包括条件判断语句中的缩进,代表了哪一个条件从句



严格注意缩进。
以上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: