if else流程判断python
getpass在pycharm中没法使用,在命令行窗口中进入python环境可使用。oop
import getpass
username = input("username:") password = getpass.getpass("password:") print(username,password)
python中缩进错误:优化
为何python中强制缩进,由于python中不须要定义结束符。省去告终束符,子代码强制缩进让结构变得更清晰。spa
最外层代码必须顶格写,否则就会报缩进错误。命令行
if else基础程序举例:debug
实例一:判断用户名密码是否正确调试
_username = 'alex' _password = 'abc123' username = input("username:") password = input("password:") if _username == username and _password == password: print("Welcom user {name} login...".format(name=username)) else: print("Ivalid username or password")
实例二:猜年龄code
# 猜年龄 age_of_oldboy = 56 guess_age = int(input("guess age:")) if guess_age == age_of_oldboy: print("you got it!") elif guess_age < age_of_oldboy: print("think bigger...") else: print("think smaller...")
while循环orm
#最简单的while循环程序举例 count = 0 while True: print("count:",count) count = count+1 #至关于count +=1
实例一:猜年龄blog
#猜年龄,共猜3次,若是3次内猜对也会结束程序 age_of_oldboy = 56 count = 0 while 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
实例二:对实例一代码的优化
#猜年龄,共猜3次,若是3次内猜对也会结束程序 age_of_oldboy = 56 count = 0 while 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
实例三:增长人性化提示,输入3次错误密码后会获得提示:尝试太屡次了。
#猜年龄,共猜3次,若是3次内猜对也会结束程序,尝试3次后获得提示:你尝试的次数过多。 age_of_oldboy = 56 count = 0 while 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: print("you have tried too many times...")
实例四:对实例三程序的优化。提示代码的判断能够直接用else。
#猜年龄,共猜3次,若是3次内猜对也会结束程序,尝试3次后获得提示:你尝试的次数过多。 age_of_oldboy = 56 count = 0 while 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 else: print("you have tried too many times...")
for循环
实例一,最简单的for循环程序
for i in range(10): print("loop",i)
等于如下:
等于如下: for i in range(0,10,1): #步长默认为1 print("loop",i)
i,临时变量
range,至关于定义了(0,1,2,3,4,5,6,7,8,9) 每循环一次i按顺序取值一次。
实例二:上节课中的while循环实例改成for循环:
#猜年龄,共猜3次,若是3次内猜对也会结束程序,尝试3次后获得提示:你尝试的次数过多。 age_of_oldboy = 56 for i in range(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...") else: print("you have tried too many times...")
实例三,打印10之内的偶数:
for i in range(0,10,2): #2为步长 print("loop",i)
实例四,优化while猜年龄程序
#猜年龄,共猜3次,尝试3次后询问是否继续,若是回答:n,则结束程序;其余则从新开始程序。 age_of_oldboy = 56 count = 0 while 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("do you want to keep guessing?") if continue_confirm != "n": count = 0
break和continue的区别,根据下面2段代码,使用debug调试功能在pycharm中运行,观察后得知
代码一:
# continue的做用是结束本次循环,不会终止for循环 for i in range(0,10): if i <3: print("loop",i) else: continue print("hehe...")
代码二:
# break是结束当前循环 for i in range(0,10): if i <3: print("loop",i) else: break print("hehe...")
循环嵌套
for i in range(0,10): print("--------",i) for j in range(10): print(j) if j >5: break
查看输出:小循环输出0-6,大循环输出0-9,brake只中断当前循环。