猜数字
#猜数字 import random i = 8 while True: a = random.randint(1, 100) while True: i -= 1 print('您还有',i,'次机会') if i == 0: exit('您的次数已经用完') b = input("请输入一个数字,输入'q'结束游戏") if b == 'q': exit('感谢参与') if b.isdigit(): b = int(b) break if a == b: print("you are right,按任意键继续,按'q'键结束游戏") break elif b < a: print('guess small') elif b > a: print('guess large') else: print("请输入数字") break
#有提示 start = 1 end = 100 import random a = random.randint(start,end) while True: guess = int(input("input a number({}-{})".format(start,end))) if guess > a: print("too big") end = guess elif guess < a: print("too small") start = guess else: print("right") break
#无次数限制
import random a = random.randint(1,10) while True: b = input("Please input a number") b = int(b) if b == a: print("you are right") elif b > a: print("too big") elif b < a: print("too small")
#有次数限制 i =6 while i >=0: i-=1 b = input("Please input a number") b = int(b) if b == a: print("you are right") break elif b > a: print("too big ,and you just have{:2} chance".format(i)) elif b < a: print("too small ,and you just have{:2} chance".format(i))
循环猜数字 import random quit = False while True: if quit: break start = input("please input start number,default:1") if start =="": start =0 end = input("please input end number,default:100") if end =="": end =100 start = int(start) end = int(end) number = random.randint(start+1,end) while True: guess = input("please input a number{}-{},input 'quit'exit,input 'rest'resume".format(start+1,end)) if guess =='quit': print('bye') quit = True break if guess == 'rest': break guess =int(guess) if guess > number: print('too big') end = guess elif guess < number: print('too small') start = guess else: print("you are right") print('try again?(yes or no)') if input() =='yes': break