基于python3.4
while循环
1 #!/usr/bin/python3 2 #-*- coding=utf-8 -*- 3 4 import random 5 import sys 6 import os 7 8 lucky_num = int(random.randint(0,9)) 9 guess_count = 0 10 os.system('clear') 11 print("---------猜数字游戏----------") 12 while guess_count < 3: 13 input_num = int(input("请输入0~9的数字:")) 14 if input_num > lucky_num: 15 print("输入的数字大了") 16 elif input_num < lucky_num: 17 print("输入的数字小了") 18 else: 19 print("Bingo!") 20 break 21 sys.exit() 22 guess_count += 1 23 24 else: 25 print("没有机会了,请下次再来~")
for循环
1 #!/usr/bin/python3 2 #-*- coding=utf-8 -*- 3 4 import random 5 import sys 6 import os 7 8 lucky_num = int(random.randint(0,9)) 9 os.system('clear') 10 print("---------猜数字游戏----------") 11 for i in range(3): 12 input_num = int(input("请输入0~9的数字:")) 13 if input_num > lucky_num: 14 print("输入的数字大了") 15 elif input_num < lucky_num: 16 print("输入的数字小了") 17 else: 18 print("Bingo!") 19 break 20 sys.exit() 21 22 else: 23 print("没有机会了,请下次再来~")
来源:https://www.cnblogs.com/hygs/p/5266043.html