优化猜年龄游戏,允许用户最多猜3次,中间猜对了,直接跳出循环
age = 26 count = 0 while count < 3:#允许用户最多猜3次 user_guess = int(input(">>:")) if user_guess == age: print("恭喜你,猜对了") break elif user_guess < age: print("try bigger") else: print("try smaller") count += 1
优化猜年龄游戏,允许用户最多猜3次,猜了3次之后,再问是否还想玩,如果用户选有,则再允许猜3次,以此往复
age = 26 count = 0 while count < 3:#允许用户最多猜3次 user_guess = int(input(">>:")) if user_guess == age: print("恭喜你,猜对了") break elif user_guess < age: print("try bigger") else: print("try smaller") count += 1 if count == 3: #超过3次,询问用户是否想继续猜 choice = input("想继续猜吗:y|Y") if choice == "y" or choice == "Y": count = 0