引入随机数:random
1)random.randit(1,10)随机生成1.10之间的整数
2)random.uniform(1,10)随机生成1.10之间的浮点数
3)random.getstate(),获取fandom的种子
改进的小游戏,
print('---------------------------------来玩一个游戏吧----------------------------') import random answer=random.randint(1,9) i=0 ##print(answer) while i<3: i+=1 temp =input('请输入代表心情的数字:') guess=int (temp) if guess==answer: print("你是猜对了!") print("你是小甲鱼的蛔虫吗?") break else: if guess<answer: print("小了!") else: print("大了!") print("游戏结束,期待下次您的选择.")
获取跟之前一样的随机数alt+P,获取上一次命令的快捷键
import random x=random.getstate() print(x) random.randint(1,10) 3 random.randint(1,10) 6 random.randint(1,10) 4 random.randint(1,10) 7 random.randint(1,10) 6 random.setstate(x) random.randint(1,10) 3 random.randint(1,10) 6 random.randint(1,10) 4 random.randint(1,10) 7 random.randint(1,10) 6
标签:10,1.10,randint,python,random,第三天,print,import From: https://www.cnblogs.com/mmjmaomaochong/p/17879384.html