python练习题-day2

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

  False

4) 5 < 4 or 3 

  3

5) 2 > 1 or 6

  True

6) 3 and 2 > 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、简述变量命名规范

  1. 使用字符数字和下划线随意组成的
  2. 不能使用python中的关键字
  3. 不能使用数字或数字开头的
  4. 不能太长
  5. 不能使用中文
  6. 不能毫无意义
  7. 推荐使用:小驼峰体.下划线
  8. 注意区分大小写

5、name = input(“>>>”) name变量是什么数据类型? 

  字符型(str)

6、if条件语句的基本结构? 

  If 判断条件:

    代码块

  elif 条件:

    代码块

  else:

    代码块(所有条件都不满足的条件下运行此代码块)

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

  While 条件:

    循环体

  else:

    代码块

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

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

True_num=66
while True:
    game_num = int(input("pls input your num:"))
    if game_num > True_num:
        print("too big,pls try again")
    elif game_num < True_num:
        print("too small,pls try again")
    else:
        print("congratution,you are right")
        break

9、在8题的基础上进行升级:

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

True_num=66
count=1
while True:
    game_num = int(input("pls input your num:"))
    if game_num > True_num:
        if count==3:
            exit("too stupid")
        else:
            print("too big,pls try again")
    elif game_num < True_num:
        if count==3:
            exit("too stupid")
        else:
            print("too small,pls try again")
    elif game_num == True_num:
        print("congratution,you are right")
        break
    count+=1

10、写代码,完成下列需求:

用户可持续输入(用while循环),用户使用的情况:

输入A,则显示走大路回家,然后在让用户进一步选择:

是选择公交车,还是步行?

选择公交车,显示10分钟到家,并退出整个程序。

选择步行,显示20分钟到家,并退出整个程序。

输入B,则显示走小路回家,并退出整个程序。

输入C,则显示绕道回家,然后在让用户进一步选择:

是选择游戏厅玩会,还是网吧?

选择游戏厅,则显示 ‘一个半小时到家,爸爸在家,拿棍等你。’并让其重新输入A,B,C选项。

选择网吧,则显示‘两个小时到家,妈妈已做好了战斗准备。’并让其重新输入A,B,C选项。

while True:
    way=input("input your way(a or b or c):").upper()
    if way == "A":
        print("road(bus or walk)")
        road_way=input(">>>")
        if road_way=="bus":
            exit("10min arrive home")
        if road_way=="walk":
            exit("20min arrive home")
    if way== "B":
        exit("path")
    if way == "C":
        print("curly(game_center of net bar)")
        curly_way=input(">>>")
        if curly_way=="game_center":
            print("half an hour beated by your father")
        if curly_way == "net bar":
            print("two hours beatedddddd by your mather")

11、写代码:计算 1 - 2 + 3 ... + (-)99 中除了88以外所有数的总和?

sum=0
for i in range(100):
    if i ==88:
        continue
    if i % 2 ==0:
        sum-=i
    if i % 2 ==1:
        sum+=i
print(sum)

#为减号时
sum=0
j=-1
for i in range(100):
    if i ==88:
        continue
    else:
        sum+=i*j
        j=-j
print(sum)

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

count=0
while True:
    uname="myfu"
    password="123"
    u,p=input("input your name and password:").split()
    count+=1
    if uname ==u and password ==p:
        exit("login success")
    else:
        if count==3:
            exit("three times error input,your count is locked")
        else:
            print("error username or password ,pls try again,%s time last"%(3-count))

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

ASCII:是最早的编码格式,一般都是1byte = 8bit

Unicode:万国码,能够容下所有国家的编码格式,占32位,缺点是比较浪 费:网络传输流量高,占内存大

Utf-8:适用于汉字,最小的占位是1byte = 8bit(指英文字母),汉字一般 是3byte=24bit

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

1bytes = 8 bit:一个字节等于八个位

15、“老男孩”使⽤UTF-8编码占⽤⼏个字节?使⽤GBK编码占⼏个字节?

utf8中占9byte,GBK中占6byte

16、制作趣味模板程序需求:等待⽤户输⼊名字、地点、爱好,根据⽤户的名字和爱好进⾏任意现实 如:敬爱可亲的xxx,最喜欢在xxx地⽅⼲xxx

name,address,hobby=input("your name address and hobby(blank):").split()
print("敬爱可亲的{},最喜欢在{}地⽅⼲{}".format(name,address,hobby))
print("敬爱可亲的{2},最喜欢在{1}地⽅⼲{0}".format(hobby,address,name))
print("敬爱可亲的{n},最喜欢在{a}地⽅⼲{h}".format(h=hobby,a=address,n=name))
print("敬爱可亲的%s,最喜欢在%s地⽅⼲%s"%(name,address,hobby))

17、等待用户输入内容,检测用户输入内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输入”,并允许用户重新输⼊并打印。敏感字符:“小粉嫩”、“大铁锤”

while True:
    use_input=input("comment:")
    if "小粉嫩" in use_input or "大铁锤" in use_input:
        print("存在敏感字符请重新输入")
    else:
        exit("评论成功")

18、单行注释以及多行注释?

 # 是单行注释

""" 多行注释"""
''' 多行注释'''

注释的作用:增加程序的可读性

19、简述你所知道的Python3和Python2的区别?

20、continue和break区别?

Continue:它是打断本次循环再在执行下一次循环

Break:打断这个循环,不再执行这个循环,然后执行这个循环后面的程序

猜你喜欢

转载自www.cnblogs.com/fumy/p/10245619.html