2.while,逻辑作业

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 

print(1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6) #True
print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 ) #False

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  

print(8 or 3 and 4 or 2 and 0 or 9 and 7) #8
print(0 or 2 and 3 and 4 or 6 and 0 or 3) #4

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 

print(6 or 2 > 1)   #6
print(3 or 2 > 1 )  #3
print(0 or 5 < 4 )  #False
print(5 < 4 or 3 )  #3
print(2 > 1 or 6 )  #True
print(3 and 2 > 1 ) #True
print(0 and 3 > 1)  #0
print(2 > 1 and 3) #3
print(3 > 1 and 0 )#0
print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2) #2

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

while 条件:

    循环体(brek,continue)

5、利用while语句写出猜大小的游戏:

设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测 的结果大了;如果比66小,则示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。 

while True:
    n = int(input("请输入一个数字:").strip())
    if n > 66:
        print("猜测的结果大了")
        continue
    elif n < 66:
        print("猜测的结果小了")
        continue
    else:
        print("猜测结果正确")
        break

6、在5题的基础上进行升级:

给用户三次猜测机会,如果三次之内猜测对了了,则显示猜测正确,退出循 环,如果三次之内没有猜测正确,则⾃自动退出循环,并显示‘太笨了了你....’。  

count = 1
while count <= 3:
    n = int(input("请输入一个数字:").strip())
    if n > 66:
        print("猜测的结果大了")
    elif n < 66:
        print("猜测的结果小了")
    else:
        print("猜测结果正确")
        break
    count += 1
else:
    print("太笨了你")

7.使用while循环输出 1 2 3 4 5 6  8 9 10  

n = 1
while n <= 10:
    if n == 7:
        n += 1
        continue
    print(n)
    n += 1

8.求1-100的所有数的和

sum = 0
count = 1
while count <= 100:
    sum += count # 累加运算
    count += 1
print(sum)

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

count = 1
while count <=100:
    if count % 2 == 1:
        print(count)
    count += 1

10.输出 1-100 内的所有偶数

count = 1
while count <= 100:
    if count % 2 == 0:
        print(count)
    count += 1

11.求1-2+3-4+5 ... 99的所有数的和.   

n = 1
sum = 0
while n < 100:
    if n % 2 == 1: #奇数
        sum += n
    elif n % 2 == 0: #偶数
        sum -= n
    n += 1
print(sum)

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

uname = "xiaoqian"
upassword = "123"
count = 1
while count <=3:
    username = input("请输入用户名:").strip()
    password = input("请输入密码:").strip()
    if uname == username and upassword == password:
        print("登录成功:")
        break
    else:
        print("用户名密码错误")
        print("你还有%s次机会" % (3 - count))
    count += 1

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

number = int(input("请输入数字"))
number = abs(number)
for i in range(2,number):
    if number % i == 0:
        print("不是质数")
        break
else:
    print("是质数")

附加题,求1-1000之内的所有质数的和

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. 今年过年不收礼啊. 收礼只收脑⽩⾦.   ==> 合法

coutent = input("输入广告语:")
if "最" in coutent or "第一" in coutent or "稀缺" in coutent:
    print("广告不和法")
else:
    print("广告合法")

15. 输⼊⼀个数. 判断这个数是⼏位数(⽤算法实现)(升级题) 


# 几位数, 让用户输入一个数. 计算这是一个几位数?
num = int(input("请输入你的数字:"))
# num = abs(num) # abs() 绝对值
# 至少是一位数
count = 0
while 1: # 死循环
    num = num // 10
    count += 1
    if num == 0:
        break
print(count)

  

posted @ 2018-11-28 21:53  等待の喵  阅读(366)  评论(0编辑  收藏  举报