while 循环:
while 条件:
代码块
执行流程:
1.判断条件是否为True。若True,执行代码块。
2.在判断条件是否为True......
3.当条件为False.跳出循环
break: 结束循环。停止当前本层循环
continue:结束当前本次循环, 继续执行下一次循环
格式化输出:
%s:字符串占位符,可以放置任何内容(数字)
%d:数字的占位符
运算符:算数运算,比较运算,逻辑运算,赋值运算
算数运算: + - * / **(次幂) %(取余) //(取整)
比较运算: == != <>(不等于) > < >= <=
赋值运算: = += -= *= /= %= **= //=
逻辑运算:
and: 并且的意思, 左右两端的值必须都是真,运算结果才是真
or: 或者的意思,左右两端的值有一个是真,运算结果就是真
not: 非的意思,原来是假,现在是真,非真即假,非假即真
1.and or not 同时存在,先算括号,然后算not,然后算and , 最后算or ==》 () > not > and > or
2. x or y : x为真, 值就是x , x 为假,值就是y
x and y : x为真,值就是y, x为假,值就是x
Homework:
1.猜数字,设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。
exp_num = 66
while True:
num = int(input('please input a num to compare:'))
if num == exp_num:
print('you right,exit the progress.')
break
elif num > exp_num:
print('you guess too lager!')
else:
print('you guess too small!')
2.在上一题的基础,设置:给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘大笨蛋’。
exp_num = 66
times = 1
while times <= 3:
num = int(input('please input a num to compare:'))
if num == exp_num:
print('you right,exit the progress.')
break
elif num > exp_num:
print('you guess too lager!')
else:
print('you guess too small!')
times = times + 1
else:
print(f'you wrong too much times,you are loser!')
3.使用两种方法实现输出 1 2 3 4 5 6 8 9 10 。
num = 1
while num <= 10:
if num == 7:
num += 1
continue
print(num)
num += 1
---------------------------------
for num in range(1,11):
if num == 7:
continue
else:
print(num)
4.求1-100的所有数的和
sum = 0
i = 1
while i < 101:
sum = sum + i
i += 1
print(sum)
---------------------------------
sum = 0
for i in range(1,101):
sum += i
print(sum)
5.输出 1-100 内的所有奇数
for i in range(1,101):
if i % 2 == 1:
print(i)
6.输出 1-100 内的所有偶数
for i in range(1,101):
if i % 2 == 0:
print(i)
7.求1-2+3-4+5 … 99的所有数的和
sum = 0
for i in range(1,100):
if i % 2 == 1:
sum += i
else:
sum -= i
print(sum)
8. 用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使用字符串格式化)
name = 'bonnie'
pwd = '123456'
count = 1
while count <=3:
user_name = input("please enter your name:")
user_psw = input("please enter your password:")
if user_name == name and user_psw == pwd:
print('login success!')
break
else:
print(f'you have login failed {count} times!')
count += 1
9.简述ASCII、Unicode、utf-8编码
ASCII 英文,每8个数字为1个单位,共有2**8个组成,可以囊括所有的英文字母、数字和符号,且只能英文
unicode 万国码, 可以表示所有文字,每32个数字为1个单位,共有2**32个组成,相比于ASCII,优点是没有局限性,可以表示所有文字和符号,缺点是由于32个数字为1个单位,比较浪费资源, 多用于内存计算处理, 比较规整
utf-8 对万国码进行精炼,对用不到的位数可以进行省略,从而起到节省空间的作用,但是都是8的倍数的数字,多用于数据传输
10.简述位和字节的关系?
1个字节等于8位
#8 bit = 1 byte
#1024 byte = 1kb
#1024kb = 1mb
#1024mb = 1gb
11.猜年龄游戏 要求:允许用户最多尝试3次,3次都没猜对的话,就直接退出,如果猜对了,打印恭喜信息并退出
count = 1
age = 28
while count <= 3:
gus_age = int(input('guess my age:'))
if gus_age == age:
print('you right')
break
else:
print(f'you guess wrong {count} times.')
count += 1
12.猜年龄游戏升级版 要求:允许用户最多尝试3次,每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y,就继续让其猜3次,以此往复,如果回答N,就退出程序,如何猜对了,就直接退出。
count = 1
age = 28
while count <= 3:
gus_age = int(input('guess my age:'))
if gus_age == age:
print('you right')
break
else:
print(f'you guess wrong {count} times.')
count += 1
if count > 3:
gus_again = input("Do you want to play it again?Y/N")
if gus_again.upper() == "Y":
count = 1
else:
break
13.判断下列逻辑语句的True,False
1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
F or T or F and T and T or F
F or T or F and T or F
F or T or F or F
T
not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
not T and T or F and T and T or F
F and T or F and T or F
F or F or F
F
14.求出下列逻辑语句的值。
8 or 3 and 4 or 2 and 0 or 9 and 7 8
8 or 4 or 0 or 7
8
0 or 2 and 3 and 4 or 6 and 0 or 3 4
0 or 3 and 4 or 0 or 3
0 or 4 or 0 or 3
4
15.下列结果是什么?
6 or 2 > 1 6
3 or 2 > 1 3
0 or 5 < 4 false
5 < 4 or 3 3
2 > 1 or 6 true
3 and 2 > 1 true
0 and 3 > 1 0
2 > 1 and 3 3
3 > 1 and 0 0
3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 2
T and 2 or T and 3 and 4 or T
2 or 3 and 3 or T
2 or 3 or T
2