格式化输出:
格式化输出%占位符 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)
View Code
![](https://oscimg.oschina.net/oscnet/cf6a778e7b5d0cbb0c4e17a9bcd8b11223f.jpg)
第二种使用方法
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程序。
![](https://oscimg.oschina.net/oscnet/385ef07675ae84be2b05ea1b4ef820b98b9.jpg)
count = 0
while count 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 5 and 2 > 1 ang 9 > 8 or 7 < 6 ) F
View Code
第二种情况 逻辑运算符前后都是数字
如果第一位非零输出第一位,如果第一位是零,输出第二位
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
View Code
数字与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)
View Code
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('要不要脸你...')
View Code
3、使用while循环输入 1 2 3 4 5 6 8 9 10
count = 1
while count |