day2_Python基础二

一、格式化输出

1、%s or %d

%:表示占位符,注意,需要在内容中显示%时,在他之前增加一个%来转义,如显示5%,则:5%%

s:表示字符串

d:表示数值digital

例子:

name = input('请输入年龄')
age = input('请输入年龄')
height = input('请输入身高')
msg = "我叫 %s, 今年 %s, 身高 %s, 学习进度是3%%s" % (name, age, height)  #  3%%s中,第一个%为转意符
print(msg)
name = input('请输入name')
age = int(input('请输入年龄'))
job = input('请输入工作')
hobbie = input('你的爱好')

msg = '''------------ info of %s -----------
Name  : %s
Age   : %d
job   : %s
Hobbie: %s
------------- end -----------------''' % (name, name, age, job, hobbie)  #  注意有两个值
print(msg)

二、初始编码

1、ASCII:4个二进制位,组成一个字节,一个ASCII码占用一个字节

2、Unicode:占用4个字节,太费空间。

3、utf-8:Unicode的升级版

最少用1个字节表示英文

2个字节表示欧洲文字

占用3个字节表示中文

三、while - else

当while被break打断时,则不走else

四、数据转换

数字 -- bool值

非零的数字 --> True;零 --> False

True --> 1; False --> 0

五、逻辑运算

()not and or

运算优先级:()>  not > and > or

作业

1、写代码:计算 1 - 2 + 3 ... +(-) 99 中除了88以外所有数的总和?
 1 # 8. 写代码:计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和?
 2 
 3 count = 1
 4 sum = 0
 5 while count < 100:
 6     if count == 88:
 7         count = count + 1
 8         continue
 9     else:
10         if count % 2 == 1:
11             sum += count
12         else:
13             sum -= count
14     count = count + 1
15 print(sum)
 1 #  写代码:计算 1 - 2 + 3 ... +(-) 99 中除了88以外所有数的总和?
 2 
 3 i = 0
 4 j = -1
 5 sun = 0
 6 while i < 00:
 7     i += 1
 8     j = -j
 9     if i == 88:
10         continue
11     else:
12         # j = -j    #  将88对应的负号也跳过去,沿用到下一个
13         sun = sun + i*j
14 print(sun)

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

 1 username = "yangxiaoer"
 2 password = "123456"
 3 i = 3
 4 while i > 0:
 5     zh = input("请输入你的账号:")
 6     i -= 1
 7     if zh == username:
 8         mm = input("请输入你的密码:")
 9         if mm == password:
10             print("验证成功.正在登陆......")
11             print('''恭喜你登陆成功!
12             欢迎用户进入
13             用户名 :%s
14             密码   :%s
15             ''' % (zh, mm))
16             break
17         else:
18             if i == 0:
19                 print("你的机会已经没了!game over 下次见!")
20                 answer = input('再试试?Y or N')
21                 if answer == 'Y':
22                     i = 3
23             print("密码错误,请重新输入")
24             print("你还有"+str(i)+"次机会")
25     else:
26         print("请输入正确的用户名!")
27         if i == 0:
28             print("你的机会已经没了!")
29             answer = input('再试试?Y or N')
30             if answer == 'Y':
31                 i = 3
32         print("你还有" + str(i) + "次机会")
33 else:
34     print('你TM要不要脸')

猜你喜欢

转载自www.cnblogs.com/lijiejoy/p/8993779.html