day02 运算符和编码
课上总结
1.while循环
while循环
while 条件:
循环体(break, continue)
break:结束当前循环
continue:结束当前循环进入下次循环
例1:
while True: content = input("请输入你要喷的内容, 输入Q退出") if content == "Q": # 退出程序 打断循环 break # 直接跳出循环 print("你对打野说:", content)
例2: ↓
循环3次
最多喷三次 count = 1 while count <= 3: # count = 1 # 次数, 死循环 content = input("请输入你要喷的内容") print("你要对上单说:", content) # 改变count count = count + 1
例3: ↓
计算 1-100之间所有的数的和
sum = 0 # sum: 0 + 1 + 2 + 3 + 4....99 + 100 count = 1 # count: 1, 2, 3, 4, 99,100, 101 while count <= 100: sum = sum + count # 累加运算 count = count + 1 print(sum)
例4: ↓
数数. 打印1-100奇数和偶数
count = 1 while count <= 100: print(count) count = count + 2 count = 1 while count <= 100: if count % 2 == 1: print(count) else: print("偶数....") count = count + 1
2. 格式化输出 %s, %d,%r 占位符
%s:字符串 %d:整数(默认只能填数字,如果不是数字会报错) %r: 匹配所有(字符串丶整数) %%:转义
例1: ↓
name = input("请输入你的名字:") address = input("请输入你来自哪里:") wife = input("请输入你的老婆:") notlike = input("请输入你不喜欢的明星:") print("我叫%s, 我来自%s, 我老婆是%s, 我不喜欢%s" % (name, address, wife, notlike))
例2: ↓
新版本python的格式化输出(3.5以上)
name = input("请输入你的名字:") address = input("请输入你来自哪里:") wife = input("请输入你的老婆:") notlike = input("请输入你不喜欢的明星:") print(f"我叫{name}, 我来自{address}, 我老婆是{wife}, 我不喜欢{notlike}")
例3: ↓
%% ,转义30%不会报错
print("我叫%s, 我已经度过了30%%的人生了" % "俞洪敏")
3.运算符 and or not
运算顺序: ()→ not →and →or
and : 并且. 左右两端同时为真. 结果才能是真 or : 或者. 左右两端有一个是真. 结果就是真 not : 非. 非真既假, 非假既真 不真-> 假 不假 -> 真
当出现 x or y的时候, 判断x是否是0 如果x==0 then y 否则返回x 当出现 x and y 的时候, 和or相反
False 当成0来看,非0即 "True"
例1: ↓
混合运算
print(3 > 4 or 4 < 3 and 1 == 1) # False print(1 < 2 and 3 < 4 or 1 > 2) # True print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) # True print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) # False print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # False print((not 2 > 1 and 3 < 4) or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) # False
例2: ↓
当出现 x or y的时候, 判断x是否是0 如果x==0 then y 否则返回x print(1 or 2) # 1 print(0 or 2) # 2 print(3 or 0) # 3 print(4 or 0) # 4
例2: ↓
当出现 x and y 的时候, 和or相反 print(1 and 2) # 2 print(0 and 3) # 0 print(3 and 0) # 0 print(4 and 0) # 0
例3: ↓
False 当成0来看 print(False and 1) print(3 > 5 or 5 < 6 and 7) print(4 > 5 or 7 and 8 < 6 or 3 and 4)
例4: ↓
成员运算
content = input("请输入你的评论:") if "马化腾" in content: # content中是否包含了xxx print("你的评论不合法") else: print("你的评论是合法的") ad = input("请输入你的广告:") if "最" in ad or "第一" in ad or "全球" in ad: print("不合法的") else: print("合法的")
课后作业
1、判断下列逻辑语句的True,False.
1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
True
2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
False
2、求出下列逻辑语句的值。
1),8 or 3 and 4 or 2 and 0 or 9 and 7
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
8
2),0 or 2 and 3 and 4 or 6 and 0 or 3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
4
3、下列结果是什么?
1)、 6 or 2 > 1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
6
2)、 3 or 2 > 1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
3
3)、 0 or 5 < 4
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
False
4)、 5 < 4 or 3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
3
5)、 2 > 1 or 6
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
True
6)、 3 and 2 > 1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
True
7)、 0 and 3 > 1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
Flase
8)、 2 > 1 and 3
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
3
9)、 3 > 1 and 0
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
0
10)、 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
2
5、利⽤while语句写出猜⼤⼩的游戏:
设定⼀个理想数字⽐如: 66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测
的结果⼤了;如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果
正确,然后退出循环。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
count = 1 num = 66 while count <= 3: user = int(input("请输入数字>>>")) if user < num: print("猜小了) elif user > num : print("猜大了) elif user == "": continue else: print("猜对了!") break count +=1
6、在5题的基础上进⾏升级:
给⽤户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循
环,如果三次之内没有猜测正确,则⾃动退出循环,并显示‘太笨了你....’。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from random import randint # 导入包 # n = randint(1, 100) # 随机产生1-100的随机数 50 n = 66 # 一开始的范围 1-100 left = 1 right = 100 count = 1 while count <= 3: num = int(input("请输入你猜的数(%s-%s):" % (left, right))) # 30 if num > n: print("猜大了") right = num # 范围缩小 elif num < n: print("猜小了") left = num # 范围缩小 else: print("猜对了") print("真聪明") break # break结束循环并不会执行到下面的else count += 1 else: # 只有当条件不成立的时候才能执行到它 print("太笨了")
7.使⽤while循环输⼊ 1 2 3 4 5 6 8 9 10
count = 0 sum = 1 while count < 10: sum = count+1 count+=1 print(sum,end=" ")
8.求1-100的所有数的和
count = 0 sum = 1 while count <= 100: sum += count count+=1 print(sum)
9.输出 1-100 内的所有奇数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
count = 1 while count <= 100: if count %2 == 0: print("偶数:", count) else: print("奇数:", count) count += 1
10.输出 1-100 内的所有偶数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
count = 1 while count <= 100: if count %2 == 0: print("偶数:", count) else: print("奇数:", count) count += 1
11.求1-2+3-4+5 ... 99的所有数的和.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
count = 1 sum = 0 while count < 100: if count %2 == 0: sum -= count else: sum += count count += 1 print(sum)
12.⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤
字符串格式化)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 正确的用户名和密码 uname = "alex" upassword = "123" count = 1 while count <= 3: username = input("请输入你的用户名:") password = input("请输入你的密码:") if username == uname and password == upassword: print("登录成功") break else: print("用户名或密码错误!") print("你还有%s次机会" % (3-count)) count += 1
13. ⽤户输⼊⼀个数. 判断这个数是否是⼀个质数(升级题).
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
sum = 0 # 1-1000之内的所有质数的和 for j in range(2, 1000): # n = int(input("请输入一个数字:")) # 9 n = j for i in range(2, n): # i 是从2到n-1的 if n % i == 0: # 整除, 不是质数 # print("不是质数") break else: # 是质数 # print("是一个质数") sum += j print(j) print(sum)
14. 输⼊⼀个⼴告标语. 判断这个⼴告是否合法. 根据最新的⼴告法来判断. ⼴
告法内容过多. 我们就判断是否包含'最', '第⼀', '稀缺', '国家级'等字样. 如果包
含. 提示, ⼴告不合法
例如, 1. ⽼男孩python世界第⼀. ==> 不合法
2. 今年过年不收礼啊. 收礼只收脑⽩⾦. ==> 合法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
s = ["最","第一","稀缺"] content = input("请输入内容>>>") for i in s: if content == "稀缺" and content == "第一" and content == "最": print('不合法的!') break else: print('合法的')
14. 输入一个数. 判断这个数是几位数(用算法实现)(升级题)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
num = int(input("请输入你的数字:")) # num = abs(num) # abs() 绝地址 # 求绝对值 if num <= 0: num = -num # 至少是一位数 count = 0 while 1: # 死循环 num = num // 10 count += 1 if num == 0: break print(count)