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