Python Learning Notes (if else process judgment, while loop, for loop)

Source: Internet
Author: User

If Else process judgment

Getpass is not available in pycharm, it can be used in the Command Line window to enter the Python environment.

Import Getpass
Username = input ("username:") password = getpass.getpass ("Password:") print (Username,password)

Indentation Error in Python:

Why force indentation in Python, because there is no need to define a terminator in Python. The Terminator is omitted, and the child Code forces the indentation to make the structure clearer.

The outermost code must be shelf written, or it will report indentation errors.

Examples of If Else Foundation programs:

Instance one: Determine if the user name password is correct

_username = ' Alex ' _password = ' abc123 ' username = input ("username:") password = input ("Password:") if _username = = Username A nd _password = = password:    print ("welcom user {name} login ...". Format (Name=username)) Else:    print ("Ivalid Username or password ")

Example two: guess age

# guess Age Age_of_oldboy = 56guess_age = Int (input ("Guess Ages:")) if Guess_age = = Age_of_oldboy:    print ("You Got it!") Elif Guess_age < age_of_oldboy:    print ("Think bigger ...") Else:    print ("Think smaller ...")

While loop

#最简单的while循环程序举例count = 0while True:    Print ("Count:", count)    count = count+1 #相当于count +=1

Example one: guess age

#猜年龄, a total of 3 guesses, if 3 times disbelief will also end the program Age_of_oldboy = 56count = 0while True:    if Count = = 3: Break    guess_age = Int (Input (" Guess Age: "))    if guess_age = = Age_of_oldboy:        print (" You Got it! ")        Break    elif Guess_age < age_of_oldboy:        print ("Think bigger ...")    else:        print ("Think smaller ...")    Count +=1

Example two: Optimization of example code

#猜年龄, a total of 3 guesses, if 3 times disbelief will also end the program Age_of_oldboy = 56count = 0while Count < 3:    guess_age = int (input ("Guess Age:"))    if G Uess_age = = Age_of_oldboy:        print ("You Got it!")        Break    elif Guess_age < age_of_oldboy:        print ("Think bigger ...")    else:        print ("Think smaller ... ")    Count +=1

Example three: Increase the user-friendly hint, enter 3 times the wrong password will be prompted: try too many times.

#猜年龄, guess 3 times, if 3 times disbelief will also end the program, try 3 times to be prompted: you try too many times. Age_of_oldboy = 56count = 0while Count < 3:    guess_age = int (input ("Guess Age:"))    if guess_age = = Age_of_oldboy:        print ("You Got it!")        Break    elif Guess_age < age_of_oldboy:        print ("Think bigger ...")    else:        print ("Think smaller ...")    Count +=1if Count = = 3:    print ("You have tried too many times ...")

Example four: The optimization of the instance three procedures. The hint code can be judged directly with else.

#猜年龄, guess 3 times, if 3 times disbelief will also end the program, try 3 times to be prompted: you try too many times. Age_of_oldboy = 56count = 0while Count < 3:    guess_age = int (input ("Guess Age:"))    if guess_age = = Age_of_oldboy:        print ("You Got it!")        Break    elif Guess_age < age_of_oldboy:        print ("Think bigger ...")    else:        print ("Think smaller ...")    count +=1else:    print ("You have tried too many times ...")

For loop

Example one, the simplest for loop program

For I in range:    print ("loop", i)

equals the following:

equals the following: For I in Range (0,10,1): #步长默认为1    print ("loop", i)

I, TEMP variable

Range, which is equivalent to defining (0,1,2,3,4,5,6,7,8,9) Each loop once I take the value once in order.

Example two: The While loop instance in the last lesson changed to a for loop:

#猜年龄, guess 3 times, if 3 times disbelief will also end the program, try 3 times to be prompted: you try too many times. Age_of_oldboy = 56for i in range (3):    guess_age = int (input ("Guess Age:"))    if guess_age = = Age_of_oldboy:        prin T ("You Got it!")        Break    elif Guess_age < age_of_oldboy:        print ("Think bigger ...")    else:        print ("Think smaller ...") else:    print ("You have tried too many times ...")

Example three, print an even number within 10:

For I in Range (0,10,2): #2为步长    print ("loop", i)

Example four, optimizing while guessing age program

#猜年龄, Guess 3 times, try 3 times to ask whether to continue, if answer: N, the end of the program, others start the program again. Age_of_oldboy = 56count = 0while Count < 3:    guess_age = int (input ("Guess Age:"))    if guess_age = = Age_of_oldboy:        print ("You Got it!")        Break    elif Guess_age < age_of_oldboy:        print ("Think bigger ...")    else:        print ("Think smaller ...")    Count +=1    if Count ==3:        continue_confirm = input ("Does want to keep guessing?")        If continue_confirm! = "N":            count = 0

The difference between break and continue, according to the following 2 code, using debug debugging function in the Pycharm run, observed that

Code One:

# Continue's role is to end this loop and not terminate the For loop for I in range (0,10):    If I <3:        print ("loop", i)    else:        continue    print ("hehe ...")

Code two:

# break is the end of the current loop for I in range (0,10):    If I <3:        print ("loop", i)    else: Break    print ("hehe ...")

Loop nesting

For I in Range (0,10):    print ("--------", I) for    J in range:        print (j)        if J >5:            break

View output: Small loop output 0-6, cycle output 0-9,brake only interrupts the current loop.

Python Learning Notes (if else process judgment, while loop, for loop)

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.