py_day_2

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

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

#2.求出下列逻辑语句的值。
8 or 3 and 4 or 2 and 0 or 9 and 7
VALUE = 8
0 or 2 and 3 and 4 or 6 and 0 or 3
VALUE = 4
3.下列结果是什么?
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
# while循环语句基本结构?

while 条件语句:
循环体(代码块)

while 条件语句:

循环体(代码块)
break #跳出循环体
continue #跳出当前循环体,继续执行下次循环,当成代码块的最后一行

while 条件语句:
循环体(代码块)
else:
循环体(代码块)

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

while True:
Ideal_num = int(input('请输入数字: '))
if Ideal_num == 66:
print('猜测结果正确')
break
elif Ideal_num > 66:
print('猜测结果大了')
elif Ideal_num < 66:
print('猜测结果小了')
#6.在5题的基础上进行升级:
'''
给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,
退出循环,如果三次之内没有猜测正确,则自动退出循环,
并显示‘太笨了你....’。
'''
count = 0
while count < 3:
Ideal_num = int(input('请输入数字: '))
if Ideal_num == 66:
print('猜测正确')
break
elif Ideal_num > 66:
print('猜测结果大了')
elif Ideal_num < 66:
print('猜测结果小了')
count += 1
print('笨死了')
#7.使用while循环输出 1 2 3 4 5 6 8 9 10
count = 1
while count < 11:
if count == 7:
count += 1
continue
print(count)
count += 1
#8.求1-100的所有数的和(两种方法)

num = 0
count = 1
while count < 101:
num += count
count += 1
print(num)

#9.输出 1-100 内的所有奇数(两种方法)
count = 1
while count < 100:
if count % 2 ==1:
print(count)
count +=1

#10.输出 1-100 内的所有偶数(两种方法)
count = 2
while count < 100:
if count % 2 ==0:
print(count)
count +=2

#11.求1-2+3-4+5 ... 99的所有数的和
count = 1
sum = 0
while count < 100:
sum1 = count % 2
if sum1 == 1:
sum = sum + count
else:
sum = sum - count
count = count + 1
print(sum)

#12.用户登陆(三次输错机会)且每次输错误时显示剩余错误次数
# (提示:使用字符串格式化)
count = 2
my_user = 'liuxiaofei'
my_psw = '111111'
while count >=0:
user = input('请输入用户名: ')
psw = input('请输入密码: ')
if my_user == user and my_psw == psw:
print('登录成功!')
break
else:
print('用户名和密码输入错误,剩余错误%s次数'%(count))
count -= 1

#13.简述ASCII、Unicode、utf-8编码关系?
'''
ascii 码 由美国开发,不支持中文, 8位 一个字母占据1个字节
Unicode 万国码,国际通用, 一个字母占据2个字节为16位, 一个中文字符占用
3个字节为24位
utf-8 最少使用8位 1个字节,欧洲使用16位2个字节, 亚洲使用24位 3个字节
'''
#14. 简述位和字节的关系?
1字节(byte) = 8位
#15.“⽼男孩”使⽤UTF-8编码占⽤⼏个字节?使⽤GBK编码占⼏个字节?
老男孩 使用UTF-8 占用 12个字节
使用GBK占用6个字节.


'''














猜你喜欢

转载自www.cnblogs.com/CrazySheldon1/p/9847562.html
今日推荐