Day1.猜年龄交互功能
#作业1
猜年龄 , 可以让用户最多猜三次!
1 age = 50 2 count = 0 3 while True: 4 user_guess = int(input("input your guess:")) 5 count +=1 6 if user_guess > age: 7 print("try smaller... ") 8 elif user_guess < age: 9 print("try bigger... ") 10 else: 11 print("you got it!") 12 break 13 if count == 3: 14 print("game over") 15 break
运行结果如下:
#作业2
猜年龄 ,每隔3次,问他一下,还想不想继续玩,y,n
1 age = 50 2 count = 0 3 while True: 4 user_guess = int(input("input your guess:")) 5 count +=1 6 if user_guess > age: 7 print("try smaller... ") 8 elif user_guess < age: 9 print("try bigger... ") 10 else: 11 print("you got it!") 12 break 13 while count %3==0: 14 user_choice = input("input your choice (y or n):") 15 if user_choice == "y": 16 break 17 elif user_choice == "n": 18 exit() 19 else: 20 print("please input y or n")
运行结果如下: