Day2作业及默写
1、判断下列逻辑语句的True,False.
1)1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
2)not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
1 优先级:() NOT AND OR 从左往右 2 1)1 > 1 or 3 < 4 or(( 4 > 5 and 2 > 1) and 9 > 8) or 7 < 6 True 3 4 2)((not 2 > 1 )and 3 < 4 )or(( 4 > 5 and 2 > 1) and 9 > 8) or 7 < 6 Flash
2、求出下列逻辑语句的值。
1),8 or 3 and 4 or 2 and 0 or 9 and 7
2),0 or 2 and 3 and 4 or 6 and 0 or 3
1 OR: 先看优先级,再看第一个数是否为0,0返回第二个数,非0返回第一个数 2 AND与OR相反 3 1),8 or (3 and 4) or (2 and 0) or (9 and 7) 4 8 or 4 or 0 or 7 --->8 5 2),0 or( (2 and 3) and 4) or( 6 and 0) or 3 6 0 or (3 and 4) or 0 or 3 --->4
3、下列结果是什么?
1)、6 or 2 > 1
2)、3 or 2 > 1
3)、0 or 5 < 4
4)、5 < 4 or 3
5)、2 > 1 or 6
6)、3 and 2 > 1
7)、0 and 3 > 1
8)、2 > 1 and 3
9)、3 > 1 and 0
10)、3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
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 3>2 or 2<4 or 3 ---> 1
4、简述变量命名规范
1 1、以字母,下划线,数字 任意组合 2 2、不能以数字开头 3 3、不能以关键字命名 4 4、区分大小写,大写为常量 5 5、命名有意义,不要太长
5、name = input(“>>>”) name变量是什么数据类型?
字符串 type(name) ---->str
6、if条件语句的基本结构?
1、if 条件:代码块 2、if 条件:代码块 else:代码块 3、if 条件:代码块 elif 条件:代码块 else:代码块 4、if 条件: 代码块 if 条件: 代码块 else: 代码块 else: 代码块
7、while循环语句基本结构?
1、while 条件:代码块 2、while 条件: 代码块 else: 代码块 注:当循环中遇到break时,跳出循环,不再执行else后语句
8、利用if语句写出猜大小的游戏:
设定一个理想数字比如:66,让用户输入数字,如果比66大,则显示猜测的结果大了;如果比66小,则显示猜测的结果小了;只有等于66,显示猜测结果正确,然后退出循环。
1 def guss_number(): 2 while True: 3 right_number = 66 4 guss = int(input("请输入您猜的数字:")) 5 if guss > right_number: 6 print("您猜的大了,请重试……") 7 elif guss < right_number: 8 print("您猜的小了,请重试") 9 elif guss == right_number: 10 print("恭喜您,猜对了……") 11 break
9) 在8题的基础上进行升级:
给用户三次猜测机会,如果三次之内猜测对了,则显示猜测正确,退出循环,如果三次之内没有猜测正确,则自动退出循环,并显示‘太笨了你....’。
1 def guss_update(): 2 counts = 1 3 while counts < 4: 4 right_number = 66 5 guss = int(input("请输入您猜的数字:")) 6 if guss > right_number: 7 print("您猜的大了,请重试……") 8 elif guss < right_number: 9 print("您猜的小了,请重试") 10 elif guss == right_number: 11 print("恭喜您,猜对了……") 12 break 13 counts +=1 14 else: 15 print("3次都错了,你太笨了……")
10、写代码,完成下列需求:
用户可持续输入(用while循环),用户使用的情况:
输入A,则显示走大路回家,然后在让用户进一步选择:
是选择公交车,还是步行?
选择公交车,显示10分钟到家,并退出整个程序。
选择步行,显示20分钟到家,并退出整个程序。
输入B,则显示走小路回家,并退出整个程序。
输入C,则显示绕道回家,然后在让用户进一步选择:
是选择游戏厅玩会,还是网吧?
选择游戏厅,则显示 ‘一个半小时到家,爸爸在家,拿棍等你。’并让其重新输入A,B,C选项。
选择网吧,则显示‘两个小时到家,妈妈已做好了战斗准备。’并让其重新输入A,B,C选项。
1 def select(): 2 print(""" 3 --------info of select----------- 4 A:走大路 5 B:走小路 6 C:绕道 7 E:坐公交 8 F:步行 9 G:游戏厅 10 H:网吧 11 -------------end----------------- 12 """,end="") 13 while True: 14 select = input("请选择你的回家方式A/B/C:").upper() 15 if select == "A" : 16 print("走大路回家了") 17 select_type = input("请选择你的回家的交通工具E/F/G:").upper() 18 if select_type == "E": 19 print("大概需要10分钟到家") 20 break 21 elif select_type == "F": 22 print("大概需要20分钟到家") 23 break 24 else: 25 print("该参数无效,请重选") 26 elif select == "B" : 27 print("走小路回家") 28 break 29 elif select == "C" : 30 print("绕道回家中……") 31 select_play = input("请选择去哪里玩G/H:" ).upper() 32 if select_play == "G" : 33 print("一个半小时到家,爸爸在家,拿棍等你.") 34 continue 35 elif select_play == "H" : 36 print("两个小时到家,妈妈已做好了战斗准备") 37 continue 38 else: 39 print("无效的参数,请重新输入") 40 continue
11、写代码:计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和?
1 def sum_all(): 2 Sum = 0 3 n = 1 4 while n < 100: 5 if n % 2 == 1 : 6 Sum += n 7 else: 8 if n == 8 : 9 n +=1 10 continue 11 Sum -=n 12 n +=1 13 return Sum
12、⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化)
1 def logging(): 2 counts = 1 3 right_user_name = "sun" 4 right_user_pwd = 123456 5 while counts <=3: 6 user_name = input("please enter your userName:") 7 user_pwd = input("please enter your password:") 8 if user_name == right_user_name and user_pwd == right_user_pwd: 9 print("%s 登陆成功"%user_name) 10 break 11 else: 12 print("用户名或密码错误,请重新输入,您还有%s次机会!"%(3-counts)) 13 counts +=1 14 else: 15 print("三次已过,账户锁定")
13、简述ASCII、Unicode、utf-8编码关系?
1 ASCII 最早出现 全表256个编码 一个字节表示一个字符 2 Unicode 因为ASCII无法表示中文或其他国家的问价故升级4个字节表示 3 一个字符,但很浪费空间 4 utf-8 为了解决浪费问题,是Unicode的子集,一个英文一个字符,一 个中文3个字符
14、简述位和字节的关系?
1 1bit(位)=8bytes(字节)
15、“⽼男孩”使⽤UTF-8编码占⽤⼏个字节?使⽤GBK编码占⼏个字节?
1 9个字节,utf-8 一个中文占3个字节,GBK 一个中文占2个字节,故6个字节
16、制作趣味模板程序需求:等待⽤户输⼊名字、地点、爱好,根据⽤户的名字和爱好进⾏任意现实 如:敬爱可亲的xxx,最喜欢在xxx地⽅⼲xxx
1 def user_hobby(): 2 name = input("输入你的名字:") 3 local = input("输入你的地点:") 4 hobby= input("输入你的爱好:") 5 print("""------------Happy of learn Python---------- 6 敬爱可亲的:%s 7 最喜欢的地方是:%s 8 最喜欢干的是:%s 9 """%(name,local,hobby))
17、等待⽤户输⼊内容,检测⽤户输⼊内容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符请重新输⼊”,并允许⽤户重新输⼊并打印。敏感字符:“⼩粉嫩”、“⼤铁锤”
1 def check_words(): 2 while True: 3 error_01 = "小粉嫩" 4 error_02 = "⼤铁锤" 5 message = input("请输入您的评论:") 6 if (error_01 or error_02) in message: 7 print("存在敏感字符:%s或%s请重新输⼊"%(error_01,error_02)) 8 continue 9 else: 10 print(message,end="") 11 break
18、单⾏注释以及多⾏注释?
1 单行注释:单引号或双引号,或# '',"",# 2 多行注释:单三引号对或双三单引号对(还可以换行) '''str''',"""str""",
19、简述你所知道的Python3和Python2的区别?
1 1、py2 默认编码为ASCII码,py3 默认为Unicode 2 2、py2 第三方库含多种语言陋习,违反简单,优美,简洁原则,py3 规范 3 3、py2 用户交互永raw_input(),py3 input() 4 4、py2 print函数打印无需括号,py3需要
20、continue和break区别?
1 break跳出整个循环,continue跳出当前循环进入下一循环
明日默写代码:
Bit,Bytes,KB,MB,GB,TB之间的转换关系。
1 1 bit(位) = 8 bytes(比特/字节) 2 1KB = 1024 bytes 3 1MB = 1024KB 4 1GB = 1024MB 5 1TB = 1024GB 6 1PB =1025TB
Unicode,utf-8,GBK,每个编码英文,中文,分别用几个字节表示。
1 ASCII 1个字节一个英文,无中文 2 GBK 2个字节一个英文,2个字节表示一个中文 3 Unicode 4个字节表示一个英文,4个字节表示一个中文 4 UTF-8 1个字节一个英文,3个字节一个中文