[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>猜数字游戏
问题描述:在1—100的范围内,进行猜数字,共有8次机会。
键入1—100之间的整数,回车看结果。根据提示,进一步猜测。
解答:代码示例:
#猜数字游戏 import random #生成随机数 secret=random.randint(1,100) print(
"猜数字游戏!1—100的整数,你最多可以猜8次") tries=1 while tries<=8: guess=int(input(
"1-100的整数,第%d次猜,请输入:"%(tries))) if guess==secret: print("恭喜答对了!你只猜了%d次! \n
就是这个:%d!"%(tries,secret)) break elif guess>secret: print("不好意思,你的数大了一点!") tries
+= 1 else: print("不好意思,你的数小了一点!") tries += 1 else: print("哎呀!怎么还没猜中!再见咯!")
运行示例:
##拓展: random()生成不同数字类型的用法
random.random() 用于生成一个随机浮点数:range[0.0,1.0)
random.uniform(a,b)
用于生成一个指定范围内的随机浮点数,a,b为上下限,只要a!=b,就会生成介于两者之间的一个浮点数,若a=b,则生成的浮点数就是a
random.randint(a,b) 用于生成一个指定范围内的整数,a为下限,b为上限,生成的随机整数a<=n<=b;若a=b,则n=a;若a>b,报错
random.randrange([start], stop [,step]) 从指定范围内,按指定基数递增的集合中获取一个随机数,基数缺省值为1
random.choice(sequence)
从序列中获取一个随机元素,参数sequence表示一个有序类型,并不是一种特定类型,泛指list,tuple,字符串等
random.shuffle(x[,random]) 用于将一个列表中的元素打乱
random.sample(sequence,k) 从指定序列中随机获取k个元素作为一个片段返回,sample函数不会修改原有序列