if else流程判断
getpass在pycharm中无法使用,在命令行窗口中进入python环境可以使用。
import getpassusername = input("username:") password = getpass.getpass("password:") print(username,password)
python中缩进错误:
为什么python中强制缩进,因为python中不需要定义结束符。省去了结束符,子代码强制缩进让结构变得更清晰。
最外层代码必须顶格写,不然就会报缩进错误。
if else基础程序举例:
实例一:判断用户名密码是否正确
_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")
实例二:猜年龄
# 猜年龄 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循环
#最简单的while循环程序举例 count = 0 while True: print("count:",count) count = count+1 #相当于count +=1
实例一:猜年龄
#猜年龄,共猜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只中断当前循环。
时间: 2024-12-19 20:13:46