变量定义的规则:
1. 变量名只能是字母、数字或下划线的任意组合。
2.变量名的第一个字符不能是数字。
3.以下关键字不能声明为变量名。
if else 流程判断:
age_of_oldboy=56 guess_age=int(input("guess age")) if guess_age==age_of_oldboy: print("yes,you got it") elif guess_age>age_of_oldboy: print("think smaller...") else: print("think older!")
while循环:
count = 0 while True: print("count:",count) count = count + 1 #count +1guess 改良版:
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("yes,you got it.") break elif guess_age>age_of_oldboy: print("think smaller...") else: print("think older!") count +=1
age_of_oldboy=56 count = 0 while count<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("think smaller...") else: print("think older!") count +=1 if count ==3: print("you have tried too many...fuck off")
最终版:if count==3: 修改为else
count = 0 while count<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("think smaller...") else: print("think bigger!") count +=1 if count==3: countine_confirm=input("Dou you want to keep guessing...? ") if countine_confirm != 'n': count =0 #else: # print("you have tried too many times...fuck off")
for i in range(...).........................................循环
for i in range (10)
print("loop",i)
隔一位:
for i in range(0,10,2)
print("loop",i)
for i in range(0,10): if i<3: print("loop",i) else: continue print("he he...") ''' --------------------------------------------分割线------------------------------------------------------------------ for i in range(10): print('-----------',i) for j in range(10): print(j) if j>5:
break
作业一:编写登录接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
count=0 while count<3: _username='Jack' _password='abc123' username=input("username:") password=input("password:") if _username==username and _password==password: print("Welcome user {name} login!".format(name=username)) break else: print("invalid username or password") count += 1 else: print("you have tried too many times!")
作业二:多级菜单
- 三级菜单
- 可依次选择进入各子菜单
- 所需新知识点:列表、字典