# 打印1到100之间可以被4整除的数 for num in range(1,101): if num % 4 == 0: print(num) for num in range(0,101,4): print(num) # 列表推导式得到一样的结果 print(list(range(0,101,4)))
# 猜数字游戏 num = 45 while 1: # while 1 比while True性能会稍微好一点点 user = int(input("请输入一个数字:")) # input默认接收的str类型 if user == num: print("恭喜你猜对了") break # 猜对了 加一个break跳出循环 elif user<= num: print("输入的数字小了") else: print("输入的数字大了")
# 生成手机号的游戏 def get_user_mobile(): """这是一个生成手机号的号段放在列表中做管理,从列表中取一个字段""" prelist = ["130","131","132","133","134","135","136","137","138","139","147","150","151","152","155","156","157","158","159","186","187","188",] return random.choice(prelist) + str(random.randint(00000000,99999999)) print(get_user_mobile())
print([num for num in range(20,60) if num % 2 == 0])
# 此处的if num % 2 == 0 要放在后面
# 随机生成一个不重复列表
print(list(set(list(str(random.randint(1,9)) for i in range(11)))))