Pytthon Foundation first day--if judgment, while loop, for loop, Python string

Source: Internet
Author: User

1. Guess The Age

User_lisa =   # defines an age count = 0  # loop Count while Count < 3:  # Cycles less than 3 times    user = input (' Please your enter ' Rname: '). Strip ()  # User input, go to both spaces if    user.isdigit ():  # If the input is a numeric string, the user        = Int (user) # is converted to an  integer type        if user = = User_lisa:  # Users enter a value equal to the defined value, exit the entire program            print (' You got it ')              exit ()          elif user > user_lisa:< c14/># user input value is greater than the defined value, prompt to lose dot            print (' try smaller ')          else:            user < User_lisa   # users enter a value less than the defined value, prompted to lose a large point            print (' Try Bigger ')    else:        print (' Try Again ')   # If user input is not a number, prompt retry    count + = 1   # Number of cycles added 1print (' Too many times ') # Too many tips

2. User Login

Method One:

Count = 0user_name = ' Lisa ' pwd = ' 123 ' While Count <3:    username =input (' Please enter your username: ')    password = Input (' Please enter your password: ')    if username = = User_name and Password = = pwd: While        True:            user_cmd = Inpu T (' Please enter your cmd: '). Strip ()            print (' The cmd is%s '%user_cmd ')            if user_cmd = = ' Q ': Break                break    Else:        print (' Login failed ')    count + = 1

Method Two:

Count = 0user_name = ' Lisa ' pwd = ' 123 ' While Count <3:    username =input (' Please enter your username: ')    password = Input (' Please enter your password: ')    if username = = User_name and Password = = pwd: While        True:            user_cmd = Inpu T (' Please enter your cmd: '). Strip ()            print (' The cmd is%s '%user_cmd)            if user_cmd = = ' Q ':                exit ()    else:        print (' Login failed ')    count + = 1

3, use while loop output 1, 2, 3, 4, 5, 6, 8, 9, ten

# use while loop output 1, 2, 3, 4, 5, 6, 8, 9, 10count = 1       # to define the initial value while count < one:  # when Count is less than 11 o'clock, execute the following code    if Count = = 7:
   # when Count equals 7 o'clock, Count is 1, and the loop is returned        by count + = 1        continue print    (count) # Prints the value of count    + = 1 # count self plus 1

Output Result:

1234568910
View Code

Pytthon Foundation first day--if judgment, while loop, for loop, Python string

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.