需求:
用户一轮有三次机会进行猜年龄游戏,每猜一次会给相应的提示告知用户应该往大点猜或者小点猜,三次机会用完以后,可选择重新再来三次机会。
思路:
首先定义一个初始年龄为25和初始次数0,然后每猜一次,次数加1,同时提示猜的年龄是太小或者太大,猜对时退出游戏,当次数等于3时,提示用户是否重新再来。
代码如下:
age_of_fww = 25 count = 0 while count < 3: guess_age = int(input("guess_age:")) if guess_age == age_of_fww: print("yes! you got it") break elif guess_age < age_of_fww: print("think bigger!") else: print("think smaller!") count+=1 if(count == 3): continue_confirm = input("do you want to keep guessing(y/n)") if continue_confirm == 'y': count = 0