格式化输出,bool值,编码初识
格式化输出:
格式化输出%占位符 s,d
格式化输出,在格式化输出中,单纯的表示%需要用%%去表示。
如果你的字符串中,用了%s或者%d这种形式,name后面的%,认为是占位,如果需要用到%,需要些%%去表示,print(‘我叫%s,今年%s,学习进度2%%’)
如果字符串中没有用到占位,那么%还是你的%。print('游戏加载80%了')
name = input('请输入名字:') age = input('请输入年龄:') sex = input('请输入性别:') msg = '我的名字是' + name + '我的年龄是' + age + '我的性别是' + sex print(msg) msg = ''' ------------ info of Alex Li ----------- Name : Alex Li Age : 22 job : Teacher Hobbie: girl ------------- end ----------------- ''' 格式化输出 %占位符 %s字符串 %d 整数 %s处理字符串 全能的 %d处理数字 只能接收数字 name = input('请输入姓名:') age = int(input('请输入年龄:')) job = input('请输入工作:') hobby=input('请输入爱好:') msg = ''' ------------ info of %s ----------- Name : %s Age : %d job : %s Hobbie: %s ------------- end ----------------- ''' % (name, name, age, job, hobby) print(msg)
第二种使用方法
dic = {
'name':'老男孩',
'age':58,
'job':'boss',
'hobby':'money',
}
msg = '''
------------ info of %(name)s -----------
Name : %(name)s
Age : %(age)d
job : %(job)s
Hobbie: %(hobby)s
------------- end -----------------
''' % dic
print(msg)
格式化输出 %占位符 %s字符串 %d 整数
%s处理字符串 全能的
%d处理数字 只能接收数字
while else 当while循环被break打断,则不走else程序。
count = 0
while count <= 5:
count += 1
print("Loop",count)
if count == 4: break
else:
print("循环正常执行完啦")
print("-----out of while loop ------")
View Code
in 可以帮我们判断xxx字符串是否出现在xxxxxxx字符串中
not in 没有出现xxx
content = input("请输入你的评论:") 马化腾是特殊字符 if "马化腾" in content: print("非法的") else: print("合法的")
not and or 计算:and是与,or是或。与0得0,或1得1
第一种情况 逻辑运算符前后都是比较运算
优先级概念:()> not>and>or,同一优先级从左至右以此计算
print(2 > 1 and 3 < 4 or 8 < 10 and 4 >5) and是与,or是或。与0得0,或1得1 print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1) T print(1 >2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8) F print(1 >1 and 3 <4 or 4> 5 and 2 > 1 ang 9 > 8 or 7 < 6 ) F
第二种情况 逻辑运算符前后都是数字
如果第一位非零输出第一位,如果第一位是零,输出第二位
and与or相反
x or y i f x True , return x, else y
print(3 or 5 ) 3 print(2 or 5 ) 2 print(0 or 5 ) 5 print(-4 or 5 ) -4
数字与bool值转化
int------bool 非零 Ture , 零 False
bool----int Ture 1 , False 0.
编码初识: 谍战片; 嘀嘀嘀嘀 高低电平 010101 电脑文件的存储,与文件的传输 0101010 初级密码本 :
ascii 字母 ,数字,特殊字符。 0000 0001 8位==1个字节 字符:组成内容的小单元。 abc a b 中国 中 国
大写字母A-Z在ASCII码中位置是65-90
小写a-z是97-122
字符:组成你看到的内容的最小单位就是字符
位:二进制中战友的位置就是位
字节:8位表示一个字节
最开始设计之初是7位,后来添加到8位.asscii最左边位是0,当时的设计者预留了1位,以便后期使用.
万国码:unicode
创建初期 16 位 两个字节表示一个字符
ASCII 由8位表示一个字节,最多只能表示256个符号,不能装中文
1个bit => 2种情况
2个bit => 4中情况
3个bit => 8种情况
8个bit => 256种情况
a : 01100001 01100001 中 : 01100001 01100001 升级 : 32 位 四个字节表示一个字符。 a :01100001 01100001 01100001 01100001 中 :01100001 01100001 01100001 01100001 资源浪费 对Unicode 升级 : utf-8 utf-8:最少用8位数去表示一个字符 a:01100001 (字母用一个字节表示。) 欧洲文字:01100001 01100001 (欧洲用2个字节表示) 亚洲文字——中:01100001 01100001 01100001(欧洲用3个字节表示) utf-16:最少用16位数去表示一个字符 gbk :国家标准。 a:01100001 中:01100001 01100001 gbk只能跟英文 8位 = 1个byte 1024bytes = 1kb 1o24kb = 1MB 1024MB = 1GB 1024 GB = 1TB
练习题:
写代码:计算1 - 2 + 3-4... + 99中除了88以外所有数的总和?
count = 1 sum = 0 while count < 100: if count ==88: count = count + 1 if count % 2 == 0: sum = sum - count else: sum = sum + count count = count + 1 print(sum)
2,用户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使?字符串格式化)
i = 0 while i < 3: username = input('请输入用户名:') password = input('请输入密码:') if username == '婉容' and password == '123': print('登录成功') break else: print('用户名或者密码错误,请重新输入,还剩%d次机会' % (2-i)) if i == 2: choice = input('是否还想试试?/Y') if choice == 'Y': i = -1 i += 1 else: print('要不要脸你...')
3、使用while循环输入 1 2 3 4 5 6 8 9 10
count = 1 while count <11: if count ==7: count = count + 1 print(count) count = count + 1
6空格8 count = 1 while count < 11: print(count) count = count + 1 if count == 7: print(' ') count += 1
4、求1-100的所有数的和
count = 1 sum = 0 while count < 101: sum = sum + count count = count + 1 print(sum)
5,输出 1-100 内的所有奇数,所有偶数
count = 1 while count < 101: print(count) count = count + 2 偶数: count = 2 while count < 101: print(count) count = count + 2
6,求1-2+3-4+5 ... 99的所有数的和
count = 1 sum = 0 while count <100: if count % 2 == 0: sum = sum - count else: sum = sum + count count = count + 1 print(sum)
7,用户登陆(三次机会重试)
i = 0 while i <3: username = input('请输入用户名:') password = input('请输入密码:') if username == "zhangsan" and password == "123": print("登录成功") break else: print('密码错误,请重新输入') i = i + 1
8,等待⽤户输⼊内容,检测⽤户输⼊内容中是否包含敏感字符?,如果存在敏感字符提示“存在敏感字符请重新输⼊”,
并允许⽤户重新输⼊并打印。敏感字符:“⼩粉嫩”、“⼤铁锤”
while True: comment = input('请输入评论:') if '小粉嫩' in comment or '大铁锤' in comment: print("存在敏感字符,请重新输入") else: print(comment) break
posted on 2018-05-26 19:49 liangliang123456 阅读(1944) 评论(0) 编辑 收藏 举报