基础相关题
1、判断下列逻辑语句的True,False.
# (1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 # T # print(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 F # print(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 # 8 # print(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 # 4 # print(0 or 2 and 3 and 4 or 6 and 0 or 3)
3、下列结果是什么?
# # 1)、6 or 2 > 1 # 6 # print(6 or 2 > 1) # # 2)、3 or 2 > 1 # 3 # print(3 or 2 > 1) # # 3)、0 or 5 < 4 # False # print(0 or 5 < 4) # # 4)、5 < 4 or 3 # 3 # print(5 < 4 or 3) # # 5)、2 > 1 or 6 # True # print(2 > 1 or 6) # # 6)、3 and 2 > 1 # True # print(3 and 2 > 1) # # 7)、0 and 3 > 1 # 0 False # print(0 and 3 > 1) # # 8)、2 > 1 and 3 # 3 # print(2 > 1 and 3) # # 9)、3 > 1 and 0 # 0 # print(3 > 1 and 0) # # 10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 # 2 # print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2)
4、while循环语句基本结构?
while 条件: 代码块(循环体) 执行流程: 1.判断条件是否为真,如果真,执行代码块 2.再次判断条件是否为真,如果真,继续执行代码块 3.当条件为假,指向 执行else 跳出循环,循环结束
5、利用while语句写出猜大小的游戏:
# 设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测
# 的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。
index = 66 flag = True while flag and True: content = int(input("请输入数字:").strip()) if content > index: print("猜大了,请重新猜") elif content < index: print("猜小了,请重新猜") else: print("恭喜你猜对了") flag = False break
# 6、在5题的基础上进行升级:
# 给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循
# 环,如果三次之内没有猜测正确,则自动退出循环,并显示‘太笨了你....’。
count = 0 flag = True while flag : if count == 3: print("太笨了不和你玩了") flag =False break content = int(input("请输入数字:").strip()) if content > 66: print("猜大了,剩余%s次"% (2-count)) elif content < 66: print("猜小了,剩余%s次"% (2-count)) else: print("对了剩余%s次"% (2-count)) flag =False break count +=1
7.使用while循环输入 1 2 3 4 5 6 8 9 10
index = 1 while index < 11: if index == 7: index += 1 continue print(index) index += 1
8.求1-100的所有数的和
index = 0 sum = 0 while index < 101: sum += index index += 1 print(sum)
9.输出 1-100 内的所有奇数
index = 0 while index < 100: if index % 2 == 1: print(index) index += 1
10.输出 1-100 内的所有偶数
if index % 2 != 1: print(index) index += 1 index = 0 while index < 101: if index % 2 == 0: print(index) index += 1
11.求1-2+3-4+5 ... 99的所有数的和
index = 0 sum = 0 while index <100: if index %2 ==1: sum +=index else: sum -= index index +=1 print(sum)
12.用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使用字符串格式化)
count = 0 flag =True while flag: if count == 3: flag =False break content = input("请输入账号:").strip() if content =="qwe": password =int(input("请输入密码:").strip()) if password ==123: print("登录成功") flag =False break else: print("密码错误,还剩余%s次"%(2-count)) else: print("账号输入错误:请重新输入,还剩余%s次"%(2-count)) count += 1
13. 用户输输入一个数. 判断这个数是否是个质数(
count = int(input("请输入数字:")) num = 2 while num < count: if count % num == 0: print("%s不是质数" % (count)) break num +=1 else: print("%s是质数" % (count)) #用户输入 num = int(input("请输入一个数字:")) #质数大于1 if num >1: #查看因子 for i in range(2,num): if (num % i==0): print(num,"不是质数") print(i,"乘于",num // i ,"是",num) break else: print(num,"是质数")
# 14. 输入一个广告标语. 判断这个个告是否合法. 根据最新的广告法来判断. 广告法内容过多. 我们就判断是否包含'最', '第一',稀缺','国家级'等字样. 如果包含. 提示, 用告不合法
# 例如, 1. ⽼男孩python世界第一. ==> 不合法
#
# 2. 今年过年不收礼啊.收礼只收脑白金. ==> 合法
flag = True while flag: content = input("请输入广告语:") if "最" in content or "第一" in content or "稀缺" in content or "国家级" in content: print("你输入的不合法") else: print(content)
14. 输入一个数. 判断这个数是几位数(用算法实现)(升级题)
number = int(input("请输入数字:").strip()) count = 0 while (number > 0): number //= 10 count += 1 print(count)