The path to Python-Basics 2

Source: Internet
Author: User

10. If Else process judgment

Illustration 1:

Import Getpass #引用getpass这个模块_username = "KK" _password = "123456" username = input ("username:") password = Getpass.getpass ("password") #getpass功能是让密码不直接显示成明文if _username = = Username and _password = = Password:print ("Welcome user {name} login ... ". Format (name = username)) #print (" Welcome User%s login ... "% username) else:print (" error ")

Note: Indent function: Child code needs indentation

Illustration 2:

#猜年龄age_of_oldboy = 65guess_age = Int (input ("Guess Age:")) if guess_age = = Age_of_oldboy:print ("Yes, you Got it.") Elif guess_age > Age_of_oldboy:print ("No, think bigger!") Else:print ("No,think smaller!")

11. While loop

Illustration 1:

#死循环count = 0while True:print ("Count", count) Count = Count + 1


Illustration 2:

Age_of_oldboy = Count = 0 #设置计数while Count < 3: #如果小于3次时循环执行 #if count = = 3: #break guess_age = int (input ("G    Uess Age: ")) if guess_age = = Age_of_oldboy:print (" Yes, you Got it. ")  Break Elif guess_age > Age_of_oldboy:print ("No, think bigger!")  Else:print ("No,think smaller!") Count + = 1else:print ("Funk off")


12. For loop

Illustration 1:

#for循环for i in range: print ("loop", i)


Answer: C:\users\documents\python>python learn01.pyloop 0loop 1loop 2loop 3loop 4loop 5loop 6loop 7loop 8loop 9


Illustration 2:

#循环猜年龄

Age_of_oldboy = 65for i in range (3): guess_age = Int (input ("Guess Age:")) if guess_age = = Age_of_oldboy:prin        T ("Yes, you Got it.")    Break Elif guess_age > Age_of_oldboy:print ("No, think bigger!") Else:print ("No,think smaller!") Else:print ("Funk off")


Illustration 3:

For I in Range (0,10,2): Print ("loop", i)



The path to Python-Basics 2

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.