10、if else 流程判断
举例说明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")
注:缩进功能:子代码需要缩进
举例说明2:
#猜年龄 age_of_oldboy = 65 guess_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循环
举例说明1:
#死循环 count = 0 while True: print ("count",count) count = count + 1
举例说明2:
age_of_oldboy = 65 count = 0 #设置计数 while count < 3:#如果小于3次时循环执行 #if count == 3: #break guess_age = int(input("guess 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 += 1 else: print("Funk off")
12、for循环
举例说明1:
#for循环 for i in range(10): print("loop ", i)
答案: C:\Users\Documents\python>python learn01.py loop 0 loop 1 loop 2 loop 3 loop 4 loop 5 loop 6 loop 7 loop 8 loop 9
举例说明2:
#循环猜年龄
age_of_oldboy = 65 for i in range(3): guess_age = int(input("guess 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!") else: print("Funk off")
举例说明3:
for i in range(0,10,2): print("loop ",i)
时间: 2024-10-21 20:06:24