运算符和编码作业及默写

1、判断下列列逻辑语句句的True,False.

  1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

  2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6

2、求出下列列逻辑语句句的值。
  1),8 or 3 and 4 or 2 and 0 or 9 and 7

  2),0 or 2 and 3 and 4 or 6 and 0 or 3

3、下列列结果是什什么?

  1)、6 or 2 > 1

  2)、3 or 2 > 1

  3)、0 or 5 < 4

  4)、5 < 4 or 3

  5)、2 > 1 or 6

  6)、3 and 2 > 1
  7)、0 and 3 > 1
  8)、2 > 1 and 3
  9)、3 > 1 and 0
  10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2

4、while循环语句句基本结构?

5、利利⽤用while语句句写出猜⼤大⼩小的游戏: 设定⼀一个理理想数字⽐比如:66,让⽤用户输⼊入数字,如果⽐比66⼤大,则显示猜测的结果⼤大了了;如果⽐比66⼩小,则显示猜测的结果⼩小了了;只有等于66,显示猜测结果 正确,然后退出循环。

6、在5题的基础上进⾏行行升级: 给⽤用户三次猜测机会,如果三次之内猜测对了了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则⾃自动退出循环,并显示‘太笨了了你....’。

7.使⽤用while循环输出123456 8910

8.求1-100的所有数的和
9.输出 1-100 内的所有奇数

10.输出 1-100 内的所有偶数
11.求1-2+3-4+5 ... 99的所有数的和.

12.⽤用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤用字符串串格式化)

13. ⽤用户输⼊入⼀一个数. 判断这个数是否是⼀一个质数(升级题).

14. 输⼊入一个广告标语. 判断这个广告是否合法. 根据最新的广告法来判断. 广 告法内容过多. 我们就判断是否包含'最', '第⼀', '稀缺', '国家级'等字样. 如果包 含. 提示, 广告不合法 


例如, 1. 老男孩python世界第一. ==> 不合法
  2. 今年过年不收礼啊. 收礼只收脑白金. ==> 合法

15. 输入一个数. 判断这个数是几位数(用算法实现)(升级题) 


明⽇日默写代码:
1. 求1-100之间所有的数的和
2. And or not的含义和特征
3. break continue的含义. 有什么区别

  1 # 1、判断下列逻辑语句的True,False.
  2 # 1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6  (True)
  3 # 2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 (False)
  4 # 2、求出下列逻辑语句的值。
  5 # 1),8 or 3 and 4 or 2 and 0 or 9 and 7   (8)
  6 # 2),0 or 2 and 3 and 4 or 6 and 0 or 3   (4)
  7 # 3、下列结果是什么?
  8 # 1)、 6 or 2 > 1  (6)
  9 # 2)、 3 or 2 > 1  (3)
 10 # 3)、 0 or 5 < 4  (False)
 11 # 4)、 5 < 4 or 3  (3)
 12 # 5)、 2 > 1 or 6  (True)
 13 # 6)、 3 and 2 > 1  (True)
 14 # 7)、 0 and 3 > 1  (0)
 15 # 8)、 2 > 1 and 3  (3)
 16 # 9)、 3 > 1 and 0  (0)
 17 # 10)、 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2  (2)
 18 
 19 
 20 # 4、 while循环语句基本结构?
 21     # while 条件语句:
 22     #     循环体
 23 
 24 # 5、利⽤while语句写出猜⼤⼩的游戏:
 25 # 设定⼀个理想数字⽐如: 66,让⽤户输⼊数字,如果⽐66⼤,则显示猜测
 26 # 的结果⼤了;如果⽐66⼩,则显示猜测的结果⼩了;只有等于66,显示猜测结果
 27 # 正确,然后退出循环。
 28 
 29 #初级玩法
 30 # num = 66
 31 # while True:
 32 #     guess = int(input('请猜测数字:'))
 33 #     if guess > num:
 34 #         print('Too bigger')
 35 #     elif guess < num:
 36 #         print('Too smaller')
 37 #
 38 #     else:
 39 #         print('you are right')
 40 #         break
 41 
 42 #升级:随机产生一个数字去猜测,直到猜测正确停止
 43 # from random import randint
 44 # num = randint(1,100)#随机产生一个1-100之间的随机整数
 45 # left = 1
 46 # right = 100
 47 # while True:
 48 #     guess = int(input('请猜测数字(%s-%s):' % (left, right)))#提示正确的范围
 49 #     if guess > num:
 50 #         print('Too bigger')
 51 #         if right < num:
 52 #             right = guess
 53 #     elif guess < num:
 54 #         print('Too smaller')
 55 #         if left > num:
 56 #             left = guess
 57 #     else:
 58 #         print('you are right')
 59 #         break
 60 
 61 #升级:随机产生一个数字去猜测,猜测3次就停止
 62 # from random import randint
 63 # num = randint(1,100)#随机产生一个1-100之间的随机整数
 64 # left = 1
 65 # right = 100
 66 # count = 0
 67 # while count < 3:
 68 #     guess = int(input('请猜测数字(%s-%s):' % (left, right)))#提示正确的范围
 69 #     if guess > num:
 70 #         print('Too bigger')
 71 #         if right < num:
 72 #             right = guess
 73 #     elif guess < num:
 74 #         print('Too smaller')
 75 #         if left > num:
 76 #             left = guess
 77 #     else:
 78 #         print('you are right')
 79 #         break #break 结束循环并不会执行到下面的 else
 80 #     count += 1
 81 # else:#只有当条件不成立的时候才能执行到它,第一次循环的时候条件就不成立也会执行它
 82 #     print('太笨了你....')
 83 # 如果不使用 else 可以这样
 84 # if count >= 3:
 85 #     print('真笨')
 86 # else:
 87 #     print('真聪明')
 88 
 89 # 6、在5题的基础上进⾏升级:
 90 # 给⽤户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循
 91 # 环,如果三次之内没有猜测正确,则⾃动退出循环,并显示‘太笨了你....’。
 92 # num = 66
 93 # count = 0
 94 # while count < 3:
 95 #     guess = int(input('请猜测数字:'))
 96 #     if guess > 66:
 97 #         print('Too bigger')
 98 #     elif guess < 66:
 99 #         print('Too smaller')
100 #     else:
101 #         print('you are right')
102 #         break
103 #     count += 1
104 # else:
105 #     print('太笨了你....')
106 
107 # 7.使⽤while循环输⼊ 1 2 3 4 5 6 8 9 10
108 #方法一
109 # count = 0
110 # while count < 10:
111 #     count += 1
112 #     if count == 7:
113 #         continue
114 #     else:
115 #         print(count)
116 #
117 #方法二
118 # count = 1
119 # while count <= 10:
120 #     if count != 7:
121 #         print(count)
122 #     count += 1
123 
124 
125 
126 # 8.求1-100的所有数的和
127 # count = 1
128 # sum = 0
129 # while count <= 100:
130 #     sum += count
131 #     count += 1
132 # print(sum)
133 
134 # 9.输出 1-100 内的所有奇数
135 # count = 1
136 # while count <= 100:
137 #     if count % 2 == 1:
138 #         print(count)
139 #     count += 1
140 
141 
142 
143 # 10.输出 1-100 内的所有偶数
144 # count = 1
145 # while count <= 100:
146 #     if count % 2 == 0:
147 #         print(count)
148 #     count += 1
149 
150 
151 
152 
153 # 11.求1-2+3-4+5 ... 99的所有数的和.
154 # count = 1
155 # sum = 0
156 # while count < 100:
157 #     if count % 2 == 1:  # 奇数加上
158 #         sum += count
159 #     else:
160 #         sum -= count  # 偶数减去
161 #     count += 1
162 # print(sum)
163 
164 # 12.⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤
165 # 字符串格式化)
166 #方法一:自己做的
167 # count = 3
168 # while count > 0:
169 #     count -= 1
170 #     name = input('Name:').strip()
171 #     password = input('Password:').strip()
172 #
173 #     if name == 'alex' and password == 'abc':
174 #         print('welcome...')
175 #         break
176 #     else:
177 #         if count != 0:
178 #             print('你还有%s 次输错机会' % count)
179 #
180 # else:
181 #     print('你已经输错三次了,无法登录')
182 #方法二:老师讲的
183 # uname = 'alex'
184 # upsw = '123'
185 # count = 1
186 # while count <= 3:
187 #     username = input('请输入你的用户名:').strip()
188 #     password = input('请输入你的密码:').strip()
189 #     if username == uname and password == upsw:
190 #         print('登录成功')
191 #         break
192 #     else:
193 #         print('用户名或密码错误!')
194 #         if 3-count > 0:
195 #             print('你还有%s 次输错机会' % (3-count))  #机会用(3-count)很好
196 #         else:
197 #             print('你已经输错三次了,无法登录')
198 #             break
199 #     count += 1
200 
201 
202 # 13. ⽤户输⼊⼀个数. 判断这个数是否是⼀个质数(升级题).
203 #质数:只能被1和它本身整除的数
204 
205 # num = int(input('请输入一个大于1的整数:'))
206 # if num == 1:
207 #     print('1不是质数也不是合数')
208 # else:
209 #     count = 2
210 #     while count < num:
211 #
212 #         if num % count == 0:
213 #             print('%s 不是质数' % num)
214 #             break
215 #         count += 1
216 #     else:
217 #         print('%s 是质数' % num)
218 
219 
220 
221 # 14. 输⼊⼀个⼴告标语. 判断这个⼴告是否合法. 根据最新的⼴告法来判断. ⼴
222 # 告法内容过多. 我们就判断是否包含'最', '第⼀', '稀缺', '国家级'等字样. 如果包
223 # 含. 提示, ⼴告不合法224 # 例如, 1. ⽼男孩python世界第⼀. ==> 不合法
225 # 2. 今年过年不收礼啊. 收礼只收脑⽩⾦. ==> 合法
226 
227 # ad = input('请输入一个广告标语:')
228 # if '最' in ad   or   '第⼀' in ad    or    '稀缺' in ad or '国家级' in ad:
229 #     print('广告不合法')
230 # else:
231 #     print('广告合法')
232 
233 ##错误示范:
234 # if '最' or '第⼀' or '稀缺' or '国家级' in ad:#这样等价于   if '最' or '第⼀' or '稀缺' or ('国家级' in ad),结果就是'最'(因为在 or 表达式中,最左边的'最'不为0,而且 in 的优先级比 or 高)
235 # if ('最' or '第⼀' or '稀缺' or '国家级') in ad:#这样等价于 if '最' in ad,结果就是判断这个表达式的值
236 #结论:上面这个表达式不加括号的话是 in 的优先级比 or 高
237 
238 
239 # 14. 输⼊⼀个数. 判断这个数是⼏位数(⽤算法实现)(升级题)240 # num = int(input('请输入一个整数:'))
241 # count = 0
242 # while True:
243 #     count += 1
244 #     if num // 10 != 0:
245 #         num //= 10
246 #         continue
247 #     else:
248 #         print('你输入的是一个%s 位数' % count)
249 #         break
250 
251 
252 
253 
254 
255 # 明⽇默写代码:
256 # 1. 求1-100之间所有的数的和
257 # count = 1
258 # sum = 0
259 # while count <= 100:
260 #     sum += count
261 #     count += 1
262 # print(sum)
263 
264 
265 # 2. And or not的含义和特征
266 # and 并且  x or y的时候, 判断x是否是0 如果x==0 then y 否则返回x
267 # or 或者   x and y 的时候, 和or相反
268 # not 非
269 '''
270 and : 并且. 左右两端同时为真. 结果才能是真
271 or : 或者. 左右两端有一个是真. 结果就是真
272 not : 非. 非真既假, 非假既真 不真-> 假  不假 -> 真
273 '''
274 # 3. break continue的含义. 有什么区别
275 # continue  停止当前本次循环. 继续执行下一次循环
276 # break  彻底的干掉一个循环
277 
278 # break: 立刻跳出循环. 打断的意思
279 # continue: 停⽌止本次循环, 继续执行下一次循环.
参考答案

猜你喜欢

转载自www.cnblogs.com/liangxiaoji/p/10056518.html