One, using the while loop guessing age game:
1 #Author:yebo2Count =03Age_of_yebo = 204 whileCount < 3:5 6guess_age = Int (input ("Guess Age:"))#Input the number entered by default is in string form, to be converted to int type7 8 ifAge_of_yebo = =Guess_age:9 Print("congratulations!")Ten Break One elifAge_of_yebo >Guess_age: A Print("Guess bigger!") - Else: - Print("Guess smaller!") theCount +=1 - Else: - Print("You tried too many times.")
Second, use for loop guessing age game:
1 #Author:yebo2 3Age_of_yebo = 204 5 forIinchRange (3):6guess_age = Int (input ("Guess Age:"))#Input the number entered by default is in string form, to be converted to int type7 8 ifAge_of_yebo = =Guess_age:9 Print("congratulations!")Ten Break One elifAge_of_yebo >Guess_age: A Print("Guess bigger!") - Else: - Print("Guess smaller!") the Else: - Print("You tried too many times.")
Third, upgrade version guess age game:
1 #Author:yebo2Count =03Age_of_yebo = 204 whileCount < 3:5 6guess_age = Int (input ("Guess Age:"))7 8 ifAge_of_yebo = =Guess_age:9 Print("congratulations!")Ten Break One elifAge_of_yebo >Guess_age: A Print("Guess bigger!") - Else: - Print("Guess smaller!") theCount +=1 - ifCount = = 3: -Continue_confirm = input ("want to keep trying?") - ifContinue_confirm! ="N": +Count = 0
Python cycle guessing age game