猜数字游戏,程序自动生成一个0-100的随机数,用户可以猜测3次,3次后可选择是否继续猜测。
#!/usr/bin/env python #-*- coding:utf-8 -*- # Li Rong Yang """ This game lets user guess he/her age. """ import random oldman_age = random.randint(0,100)#生成一个随机数 count = 0 while True: if count == 3: break user_guess = int(input("Please guess age. "))#提示用户输入一个猜测的数字 if user_guess > oldman_age: #判断猜测数字和生成数字是否一致 print("large...") elif user_guess < oldman_age: print("smaller...") else: print("He/Her age is {_age}.Yes you got it. very nice".format(__age = user_guess)) break count += 1 #计数+1 if count == 3: #判断用户猜测乐几次,猜测3次后提示用户是否继续 inquiry = input("Do want to continue? Y/N") if inquiry != "N" and inquiry != "n": count = 0 if user_guess != oldman_age: print("Welcome to use login this game.")