Python learns _2_ by guessing the digital learning cycle, where three different implementations of code

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.