Python syntax for... esle and while... else, pythonwhileelse

Source: Internet
Author: User

Python syntax for... esle and while... else, pythonwhileelse

The syntax for... else and while... else of Python is one of the most common and misunderstood syntax features in Python.

The for and while loops in Python all have an optional else Branch (similar to the if statement and try statement), which is executed after the loop iteration is completed normally. In other words,If we do not exit the loop in other ways than normal, the else branch will be executed. That is, there are no break statements, no return statements, or exceptions in the loop body.

1. break status

Next we will try to execute break exit in the loop

Lucky_number.py

Lucky_num = 19
# First define a lucky number, which is clear.
# Action = True
Guess_count = 0
While guess_count <3:
# Restrict user input
Input_num = int (input ("Input the guess num :"))
# If lucky_num = input_num:
# Print ("Bingo! ")
# Action = False
# Use break to force the terminal to loop to avoid endless Loops
If input_num> lucky_num:
Print ("The real number is smalller! ")
Elif input_num <lucky_num:
Print ("The real number is bigger ...")
Else:
Print ("Bjngo !... ")
Break
Guess_count + = 1
Else:
Print ("Too required retrys! ")
First, we do not perform the test three times:
Input the guess num: 1
The real number is bigger...
Input the guess num: 2
The real number is bigger...
Input the guess num: 100
The real number is smalller!
Too required retrys!
The result shows that the else branch Statement (Too has retrys!) is executed !), The user is prompted to enter too many times. Normally, the else statement is executed. Let's take a look.
Abnormal Exit:
Users' Guesses are correct:
Input the guess num: 1
The real number is bigger...
Input the guess num: 19
Bjngo !...
From the above results, we can see that after the user guessed the number, break launched the while... else statement, and the else statement was not executed normally.
2. return

# First define a lucky number, which is clear.
# Action = True

# Restrict user input. Exit the loop if there are no guesses for three times
Def count ():
Lucky_num = 19
Guess_count = 0
While guess_count <3:
Input_num = int (input ("Input the guess num :"))
# If lucky_num = input_num:
# Print ("Bingo! ")
# Action = False
# Use break to force the terminal to loop to avoid endless Loops
If input_num> lucky_num:
Print ("The real number is smalller! ")
Elif input_num <lucky_num:
Print ("The real number is bigger ...")
Else:
Print ("Bjngo !... ")
Return guess_count

Guess_count + = 1
Else:
Print ("Too required retrys! ")

Numbers = count ()
Print (numbers)
Since the rerturn statement can only be used in functions and classes, I have defined a function to see if we can terminal the else statement when the input is correct.
(1) The input to call this function three times is incorrect.
Input the guess num: 1
The real number is bigger...
Input the guess num: 1
The real number is bigger...
Input the guess num: 1
The real number is bigger...
Too required retrys!
None
As can be seen from the results, when we input three times, the else statement runs normally, because all input is not normal and does not affect the else statement.
(2) The user guessed the correct situation:
Input the guess num: 1
The real number is bigger...
Input the guess num: 19
Bjngo !...
1
The result shows that the user guessed the error for the first time and guessed the error for the second time. Then, the user returned the guess_count variable numbers without running the else statement.
Conclusion: If you want to exit the loop when the user inputs the correct information and prompt the user when the number of inputs is too large, you can use the while... else... break statement.
Number of inputs
When the limit is exceeded, the user is reminded when no correct answer is received. If the cycle is exited and no reminder is printed, the break can be used for interruption when the user inputs the correct information.
Remember:The for and while loops in Python all have an optional else Branch (similar to the if statement and try statement), which is executed after the loop iteration is completed normally. In other words, ifWe
Otherwise, the else branch will be executed.
.
That is, there are no break statements, no return statements, or exceptions in the loop body.









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.