03 常用技巧介绍-(格式化、编码、while else、运算符

2019-08-25  本文已影响0人  武漂的小丙

1 格式化输出

templateMsg = '''
Name : %s
Age : %d
job : %s
Hobbie :%s
study plan: 3%%
'''
name = input('请输入姓名:')
age = input('请输入年龄:')
job = input('请输入职业:')
hobbie = input('请输入爱好:')

msg = templateMsg %(name, int(age), job, hobbie)

print(msg)

'''输出结果如下:

请输入姓名:程帅
请输入年龄:22
请输入职业:程序员
请输入爱好:打篮球

Name : 程帅
Age : 22
job : 程序员
Hobbie :打篮球
study plan: 3%
'''

2 while else搭配

count = 0
while count <= 5:
    count += 1
    if count == 3: break;
    print("Loop", count)
else:
    print('循环正常执行完成')
print("-------out of while loop--------------")

'''输出结果如下:
Loop 1
Loop 2
-------out of while loop--------------

Process finished with exit code 0
'''
'''
1. 正确输入用户名和密码,打印“登录成功!”
2. 用户名和密码输错,打印“登录失败,请重新登录!”
3. 用户名和密码输错三次,提示用户:“密码错误次数3次,输入yes重试:”
  3.1 输入yes:提示“要不要脸啦!”
  3.2 程序结束
'''

i = 0
while i < 3:
    username = input("请输入账号:")
    password = input("请输入密码:")
    if username == 'root' and password == '123456':
        i = 3
        print('登录成功!')
        continue
    else:
        print('登录失败,请重新登录!')
    if 2 - i <= 0:
        open = input('密码错误次数3次,输入yes重试:')
        if open != 'yes':
            break
    i += 1
else:
    print('要不要脸啦!')

3 编码

8bit = 1byte
1024byte = 1KB
1024KB = 1MB
1024MB = 1GB
1024GB = 1TB
1024TB = 1PB
1024PB = 1EB
1024EB = 1ZB
1024ZB = 1YB
1024YB = 1NB
1024NB = 1DB
常⽤到TB就够了

4 运算符

988316-20170918164114681-450791355.png 988316-20170918164226540-2018438327.png 1566738810983.png
上一篇 下一篇

猜你喜欢

热点阅读