猜数字小游戏

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


严格注意缩进。

以上

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值