Lucky_number = 57
def First_way ():
"" This is the first way:
1, with while loop;
2, There are cyclic counting variables;
3. Define the Guess_number variable outside of the while Statement.
"""
Global Lucky_number
guess_number = int ("0")
Loop_count = 0
While guess_number! = Lucky_number and Loop_count <= 2:
guess_number = int (input ("input Number:"))
if Guess_number = = Lucky_number:
Print ("you are Right!")
Break
elif Loop_count = = 2:
Print ("Your is Fail!")
Break
Elif Guess_number > Lucky_number:
Print ("Your input number to Big")
Loop_count + = 1
Elif Guess_number < Lucky_number:
Print ("Your input number to Small")
Loop_count + = 1
def Second_way ():
"" This is the second way to achieve this:
1, with while loop;
2, There are counting variables;
3, Guess_number in while internal;
4, increase the running variable judge whether guessed correctly, actually can in method one judgment is Better.
"""
Global Lucky_number
running = True
Loop_count = 0
While running and Loop_count < 3:
guess_number = int (input ("input Number:"))
if Guess_number = = Lucky_number:
Print ("you are Right!")
running = False
elif Loop_count = = 2:
Print ("Your is Fail!")
Break
Elif Guess_number > Lucky_number:
Print ("Your input number to Big")
Loop_count + = 1
Elif Guess_number < Lucky_number:
Print ("Your input number to Small")
Loop_count + = 1
def Third_way ():
"" This is the third way to achieve this:
1, with for loop;
2, no separate definition of the count variable;
3, Guess_number in for internal; optimal Solution.
"""
Global Lucky_number
For I in range (3):
guess_number = int (input ("input Number:"))
if Guess_number = = Lucky_number:
Print ("you are Right!")
Break
elif i = = 2:
Print ("Your is Fail!")
Elif Guess_number > Lucky_number:
Print ("Your input number to Big")
Elif Guess_number < Lucky_number:
Print ("Your input number to Small")
Third_way ()
Python learns _2_ by guessing the digital learning cycle, where three different implementations of code