day2 作业

1. while 循环

  while 条件:

    代码块(循环体)

  else:

    代码块

   执行顺序: 判断条件是否为真,如果为真,执行循环体...否则执行else语句

2. break 和continue

  break  是打断语句,跳出循环体.

  continue  是中止本次循环,继续循环语句.

3. 格式化输出

  %s   占位字符串  

  %d 占位数字  

4. 运算符

  算数运算

  比较运算 

  赋值运算

  逻辑运算

    and  : 并且的含义,and两端都必须为真,才为真,有一个是假的,便是假的.

    or     :    或者的含义,or两端有一个为真,便是真的,否则为假

    not   :    非假既真,非真既假

  顺序:  () --> not --> and --> or

  

  x or y :    如果x是真,则输出x,如果x为假,则输出y

    and则相反

  True:非零

  False:零

5.编码

  1.ascii  最早的编码,至今仍在使用,8比特一个字节

  2.gbk 国标码 16比特2个字节

  3, unicode 万国码 ,32比特4个字节

  4, utf-8  可变长度的unicode

    英文 8比特1个字节

    欧洲文 16比特2个字节

    汉语  24比特3个字节

day2 作业,

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

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

  #解题思路: 解题顺序是先()再not再and在or ,从左到右

    True.

  (2). 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

       值为 8

  (2) . 0 or 2 and 3 and 4 or 6 and 0 or 3

       值为 4

3.   下列结果是什么?

    1)、6 or 2 > 1      值为 6

    2)、3 or 2 > 1  值为3

    3)、0 or 5 < 4  值为 0

    4)、5 < 4 or 3  值为3

    5)、2 > 1 or 6  值为1

    6)、3 and 2 > 1  值为1

    7)、0 and 3 > 1  值为0

    8)、2 > 1 and 3  值为3

    9)、3 > 1 and 0  值为0

    10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2    值为2

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

    while 条件:

      代码块(循环体)

    else:

      代码块

    # 如果条件为真,执行代码块,直到不满足条件,跳出循环体,执行else语句

5.   用while语句写出猜大小的游戏

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

dream_number = 66
while True > 0:
count = int(input("请输入数字:"))
if count == 66:
print("猜对啦,")
break
elif count > 66:
print("猜大啦.")
elif count < 66:
print("猜小啦.")
else:
print("请你输入正确的数字:")

6,  在第五题的基础上进行升级: 给用户三次三次猜测机会,如果三次内猜对啦,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则退出循环,并显示"太笨了你...."

dream_number = 66
count = 3
while count > 0:
num = int(input("请你输入数字:"))
if num != 66:
print("猜错啦.")
count -= 1
elif num == 66:
print("猜对啦.")
break
elif count == 0:
print("你也太笨啦.")

7,   使用while循环输入1234568910.  

count = 0
while count < 10:
count += 1
if count == 7:
pass
else:
print(count)

8,  求出1-100的所有的数.

count = 0
sum = 0
while count < 100:
count += 1
sum += count
print(sum)

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

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

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

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

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

count = 0
sum1 = 0
sum2 = 0
while count < 100:
count += 1
num = count % 2
if num == 1:
sum1 += count
else:
sum2 += count
print(sum1-sum2)

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

dream_number = 66
count = 3
while count > 0 :
num = int(input("请你输入数字:"))
if num == 66:
print("猜对啦。")
break
elif num != 66:
count -= 1
print("猜错啦。")
print("你还有%s次机会"%(count))
elif count == 0:
print("你也太笨啦。")
break

13 , 用户输入一个数,判断这个数是否是一个质数.  

while True:
count = int(input("请你输入个数,判断是否为质数:"))
if count % 2 == 1:
print("这是个质数。")
else:
print("这不是个质数。")

  

14, 输入一个广告语,判断是否合法.出现"第一","最","稀缺",若果包含就不合法,否则就合法.


while True:
talk = input("请你输入一句广告标语:")
if "最" in talk:
print("广告不合法")
continue
elif "第一" in talk:
print("广告不合法")
continue
elif "稀缺" in talk:
print("广告不合法")
continue
else:
print("广告合法")
continue

15 , 输入一个数,判断这个数是几位数.
count = 0
num = int(input("请你输入这个数:"))
while num >=1 :
num //= 10
count +=1
print(count)
while True:
talk = input("请你输入一句广告标语:")
if "最" in talk:
print("广告不合法")
continue
elif "第一" in talk:
print("广告不合法")
continue
elif "稀缺" in talk:
print("广告不合法")
continue
else:
print("广告合法")
continue

猜你喜欢

转载自www.cnblogs.com/marry215464/p/9383360.html