作业(二)

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

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

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 Flase

4)、5 < 4 or 3

3

5)、2 > 1 or 6

1 True

6)、3 and 2 > 1

1 True

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 条件:
	print(  )#执行语句

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

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

i = int(input('请输入数字:'))
if i < 66:
    print('你猜小了')
elif i > 66:
    print('你猜大了')
else:
    print('你猜对啦!')

答案:

f = 66
while 1:
    num = int(input('请输入一个数字:'))
    if f == num:
        print('猜测正确')
        break
    elif f > num:
        print('小了')
    else:
        print('大了')

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

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

count=0
while count<3:
    i = int(input('请输入数字:'))
    count += 1
    if count >= 3:
        print('你太笨了')
    elif i < 66:
        print('你猜小了')
        continue
    elif i > 66:
        print('你猜大了')
        continue
    else:
        print('你猜对啦!')

答案:

f = 66
count = 0
while 1:
    num = int(input('请输入一个数字:'))
    if count == 2:
        print('太笨了你')
        break
    if f == num:
        print('猜测正确')
        break
    elif f > num:
        print('小了')
        count += 1
    else:
        print('大了')
        count += 1

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

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

答案:

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

8、求1-100的所有数的和(三种方法)

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

答案:

方法一:
flag = True
count = 1
sum = 0
while flag:
    sum += count
    count += 1
    if count > 100:
        flag = False
print(sum)

方法二:
num = 1
sum = 0
while num < 101:
    sum += num
    num += 1
print(sum)

方法三:
sum = 0
for i in range(101):
    sum += i
print(sum)

9、输出 1-100 内的所有奇数(两种方法)

#输出 1-100 内的所有奇数
count=1
while count <99:
    count=count+2
    print(count)

答案:

方法一:
for i in range(1,101, 2):
    print(i)
    
方法二:
count = 1
while count < 101:
    if count % 2 == 1:
        print(count)
        count += 2
    else:
        count += 2

10、输出 1-100 内的所有偶数(两种方法)

#输出 1-100 内的所有偶数
count=0
while count <=100:
    count=count+2
    print(count)

答案

方法一:
for i in range(2,101, 2):
    print(i)
    
方法二:
count = 2
while count < 101:
    if count % 2 == 0:
        print(count)
        count += 2
    else:
        count += 2

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

jcount=1
ocount=0
he=0
while jcount <100 and ocount<100:
    he=jcount - ocount
    jcount=jcount+1
    ocount=ocount+2
print(he)

答案:

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

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

#用户登陆(三次机会重试)
i=0
while i<3:
    usename = input('请输入你的用户名:')
    password = int(input('请输入你的密码:'))
    if usename== 'Q'and password == 123:
        print('登录成功')
        quit()
    else:
        print('登录失败')
    i=i+1

答案:

name = 'BlameKidd'
pwd = '123123'
count = 2
while count >= 0:
    name_input = input('请输入用户名:')
    pwd_input = input('请输入密码:')
    if name == name_input and pwd == pwd_input:
        print('登录成功')
    else:
        print('用户名或密码错误,还剩%s次输入机会' % (count))
    count -= 1

13、简述ASCII、Unicode、utf-8编码关系?

  • ascii码:(阿斯克码,256位)
  • Unicode:1个字节表示所有的英文,特殊字符,数字等
  • utf-8:一个中文用3个字节表示,24位,包含所有的中文

答案:

  • ASCII码:
    英文:8位
  • Unicode:
    英文:32位
    中文:32位
  • UTF-8:
    英文:8位
    欧洲文字:16位
    中文:24位

14、简述位和字节的关系?

位=bit
8bit=1字节

15、“你好吗”使⽤UTF-8编码占⽤⼏个字节?使⽤GBK编码占⼏个字节?

9个字节 / 6个字节

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值