一、常量
AGE_OF_OLDBOY = 73
AGE_OF_OLDBOY = 74
print(AGE_OF_OLDBOY)
二、基本运算符补充
-
算术运算
print(10 /3) print(10 //3) print(10 **2)
-
赋值运算
增量赋值
age = 18 age+=1 #age = age + 1 print(age) age = 18 age/=3 #age = age / 3 print(type(age))
-
交叉赋值
#第一种方法 x = 10 y = 20 temp = x x = y y = temp print(x,y) #第二种方法 x,y = y,x print(x,y)
-
链式赋值
x=y=z=10 print(id(x)) print(id(y)) print(id(z))
-
解压赋值
list1 = [1,2,3,4,5] *_,a,b = list1 #a=4,b=5 a,*_,b = list1 #a=1,b=5 a,b,*_ = list1 #a=1,b=2 print(a,b)
三、流程控制之if判断
-
语法1:
""" if 条件: 代码1 代码2 代码3 """ age_of_bak = 30 print('start.......') inp_age = input('>>>:') if age_of_bak == inp_age: print('猜对了') print('end.......')
-
语法2:
""" if 条件: 代码1 代码2 代码3 else: 代码1 代码2 代码3 """ age = 38 gender = 'male' is_beautiful = True if age >= 18 and age <= 25 and gender == 'female' and is_beautiful: print('开始表白。。。。') else: print('阿姨好')
-
语法3:
""" if 条件1: 代码1 代码2 代码3 elif 条件2: 代码1 代码2 代码3 elif 条件3: 代码1 代码2 代码3 else 条件4: 代码1 代码2 代码3 """ """ 如果: 成绩>=90,那么:优秀 如果成绩>=80且<90,那么:良好 如果成绩>=70且<80,那么:普通 其他情况:很差 """ score = input('your score>>: ') score = int(score) if score >= 90: print('优秀') elif score >= 80: print('良好') elif score >= 70: print('普通') else: print('很差')
-
语法4:
""" if 条件1: 代码1 代码2 代码3 if 条件2: 代码1 代码2 代码3 """
-
语法5:
""" if 条件1: if 条件2: 代码1 代码2 代码3 代码1 代码2 代码3 """ age = 18 gender = 'female' is_beautiful = True is_successful = True if age >= 18 and age <= 25 and gender == 'female' and is_beautiful: print('开始表白。。。。') if is_successful: print('在一起') else: print('我逗你玩呢。。。') else: print('阿姨好')
四、流程控制之while循环
-
while循环:条件循环
基本语法
""" while 条件: 代码1 代码2 代码3 """ #示范: name_of_bk = 'egon' pwd_of_bk = '123' tag = True while tag: inp_name = input('your name>>: ') inp_pwd = input('your password>>: ') if name_of_bk == inp_name and pwd_of_bk == inp_pwd: print('login successful') tag = False #other code 会被打印 else: print('username or password error') print('other code...')
# while+break:break代表结束本层循环 name_of_bk = 'egon' pwd_of_bk = '123' while True: inp_name = input('your name>>: ') inp_pwd = input('your password>>: ') if name_of_bk == inp_name and pwd_of_bk == inp_pwd: print('login successful') break #结束本层循环other code不会被打印 else: print('username or password error') print('other code...')
# while+continue:continue代表结束本次循环,直接进入下一次 count = 1 while count < 6: if count == 3: count+=1 continue print(count) count += 1
#输入三次退出 name_of_bk = 'egon' pwd_of_bk = '123' count = 0 while True: if count == 3: print('输错的次数过多。。。') break inp_name = input('your name>>: ') inp_pwd = input('your password>>: ') if inp_name == name_of_bk and inp_pwd == pwd_of_bk: print('login successful') break else: print('username or password error') count += 1 # count=3 输错3次
#while+else count = 0 while count < 5: count += 1 print(count) else: print("else的子代块只有在while循环没有被break打断的情况下才会执行") #此时会执行else count = 0 while True: if count == 10: break print(count) count += 1 else: print("else的子代块只有在while循环没有被break打断的情况下才会执行") #此时else不会执行
小练习:
name_of_bk = 'egon' pwd_of_bk = '123' count = 0 tag = True while tag: if count == 3: print('输错的次数过多。。。') break inp_name = input('your name>>: ') inp_pwd = input('your password>>: ') if inp_name == name_of_bk and inp_pwd == pwd_of_bk: print('login successful') while tag: print(""" 0 退出 1 购物 2 支付 3 查看购物 """) cmd = input('>>>: ') if cmd == '0': tag = False continue if cmd == '1': print('购物。。。。。。。') elif cmd == '2': print('支付。。。。。') elif cmd == '3': print('查看购物车') else: print('输入的指令错误') else: print('username or password error') count += 1 # count=3 输错3次
while 练习题
#1. 使用while循环输出1 2 3 4 5 6 8 9 10
#2. 求1-100的所有数的和
#3. 输出 1-100 内的所有奇数
#4. 输出 1-100 内的所有偶数
#5. 求1-2+3-4+5 ... 99的所有数的和
#6. 用户登陆(三次机会重试)
#7:猜年龄游戏
要求:
允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
#8:猜年龄游戏升级版
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
练习题答案:
习题1:
count = 0
while count < 10:
count+=1
if count == 7:
continue
print(count)
count = 1
while count <= 10:
if count == 7:
count+=1
continue
print(count)
count += 1
练习题2:
count = 1
sum = 0
while count <= 100:
sum += count
count += 1
print(sum)
练习题3:
count=1
while count <=100:
if count %2 == 1:
print(count)
count+=1
练习题4:
count=1
while count <=100:
if count %2 == 0:
print(count)
count+=1
练习题5:
res = 0
count = 1
while count < 100:
if count % 2 == 0:
res -= count
else:
res += count
count += 1
print(res)
mum = 0
for i in range(100):
if i % 2 ==0:
mum = mum - i #i的值是0 2 4 6 8
else:
mum = mum + i #i的值是1 3 5 7 9
print(mum)
练习题6:
name = 'egon'
password = "123"
count = 0
while count < 3:
input_name = input('请输入用户名:')
input_password = input('请输入密码:')
if name == input_name and password == input_password:
print('login success')
break
else:
print('用户名或密码错误')
count += 1
练习题7:
name_of_bk = 'egon'
pwd_of_bk = '123'
count = 0
while True:
if count == 3:
print('输错的次数过多。。。')
break
inp_name = input('your name>>: ')
inp_pwd = input('your password>>: ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
break
else:
print('username or password error')
count += 1 # count=3 输错3次
练习题8:
name_of_bk = 'egon'
pwd_of_bk = '123'
count = 0
while True:
if count == 3:
choice = input('继续(Y/N?)>>: ')
if choice == 'Y' or choice == 'y':
count = 0
else:
break
inp_name = input('your name>>: ')
inp_pwd = input('your password>>: ')
if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
print('login successful')
break
else:
print('username or password error')
count += 1 # count=3 输错3次