重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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")
举例说明3:
#循环猜年龄,结束提示是否继续猜
age_of_oldboy = 65 count = 0 while count < 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 if count == 3:#猜了三次还没有猜到询问用户是否要继续猜 coutine_confim = input("do you want to keep guessing?:") if coutine_confim != "n": count = 0 #else:#这个就用不到了 # print("Funk off")
注:
continue:跳出本次循环,继续到下次循环
break:结束整个循环
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)
举例说明4:
#大循环i执行1次,小循环执行10次 for i in range(10): print('-----------',i) for j in range(10): print (j) if j > 5:#小循环大于5就停止 break
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。