Python basic while loop and if judgment, python basic whileif

Source: Internet
Author: User

Python basic while loop and if judgment, python basic whileif

 Wlile Loop

While True indicates that it is always True. No matter what conditions, it will be executed downward. Below is an example of writing.

#! /Usr/bin/env pythonage = 24 # assign a value to age while True: # enter The loop inputting = int (input ("The input number is :")) # Save the user output to the variable inputting if inputting = age: # Compare print ("Guessed it !!! ")
Break if inputting <age: print ("Is to small !!! ") Else: print (" Is to big !!! ")

Write a user-friendly example. In the above example, all the conditions of while True are True. In the following example, You can execute the following code only when the conditions are met. In the following example, after you enter three times, the system will tell you whether to continue after the input times are too many. Press yes to exit and Press no.

#! /Usr/bin/env pythonage = 24 count = 0 # counter while count <3: # The count value is less than 0 to execute the following code. Inputting = int (input ("The input number is:") # Save The user output to The inputting variable. int is The data type. If inputting = age: print ("Guessed it !!! ") # Compare the value of the variable to break # if the output value Is equal to the value of age, print the output, and then destroy the loop if inputting <age: print (" Is to small !!! ") Else: print (" Is to big !!! ") Count + = 1 # Add a while count = 3 every cycle: # If count is equal to 3, run the following code warning = input (" Whether or not to continue? (Yes no) ") # The warning message is displayed, and the user output value is included in the if warning = 'no': # destroy the loop by no, yes continue count reset returns the first loop. Other returns let you enter the information break elif warning = 'yes': count = 0 else: print ("yes or no ")

If judgment

As the name suggests, "if" is just what it is. In the following example, the if statement is used. if the user name and password are correct, the welcome information is displayed. if the user name and password are incorrect, a message is displayed, indicating that the user name and password are incorrect. Of course, continuous input can be associated with the above while loop.

#! /Usr/bin/env pythonusername = input ("username:") password = input ("password:") _ username = ("admin ") # store the username and password in _ username and _ password = ("abc") if username = _ name and password = _ password: # print ("Welcome {name} login .... ". format (name = username) # {name} is a placeholder. format indicates his real identity. Else: print ("Invalid username password ")

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.