Chapter3
不带刷新的进度条
import time
scale = 10
print("开始执行".center(30, "-"))
for i in range(scale + 1):
a, b = '**' * i, '..' * (scale - i)
c = (i / scale) * 100
print("%{:3.0f}[{}->{}]".format(c, a, b))
time.sleep(0.1)
print("执行结束".center(20, "-"))
-------------开始执行-------------
% 0[->....................]
% 10[**->..................]
% 20[****->................]
% 30[******->..............]
% 40[********->............]
% 50[**********->..........]
% 60[************->........]
% 70[**************->......]
% 80[****************->....]
% 90[******************->..]
%100[********************->]
--------执行结束--------
带刷新的进度条
import time
scale = 50
print("执行开始".center(scale // 2, "-"))
t0 = time.perf_counter()
for i in range(scale + 1):
a = '*' * i
b = '.' * (scale - i)
c = (i / scale) * 100
t = time.perf_counter() - t0
print("\r{:3.0f}%[{}->{}]{:.2f}s".format(c, a, b, t), end='')
time.sleep(0.05)
print("\n" + "执行结束".center(scale // 2, "-"))
-----------执行开始----------
100%[**************************************************->]2.52s
-----------执行结束----------
凯撒密码
plaincode = input("请输入明文:")
for p in plaincode:
# ord()返回单个字符的Unicode编码
if ord("a") <= ord(p) <= ord("z"):
print(chr(ord("a") + (ord(p) - ord("a") + 3) % 26), end="")
elif ord("A") <= ord(p) <= ord("Z"):
print(chr(ord("A") + (ord(p) - ord("A") + 3) % 26), end="")
else:
print(p, end="")
请输入明文:ABCxyz
DEFabc
天天向上
import math
day_up = math.pow((1.0 + 0.001), 365)
day_down = math.pow((1.0 - 0.001), 365)
print("天天向上{:.2f},天天下降{:.2f}".format(day_up, day_down))
天天向上1.44,天天下降0.69
天天向上 有双休
import math
day_up, day_factor = 1.0, 0.01
for i in range(365):
if (i) % 7 in [6, 0]:
day_up = day_up * (1 - day_factor)
else:
day_up = day_up * (1 + day_factor)
print("向上5天向下一天{:.2f}".format(day_up))
向上5天向下一天4.63
天天向上与天天下降
import math
day_up = math.pow((1.0 + 0.005), 365)
day_down = math.pow((1.0 - 0.005), 365)
print("天天向上{:.2f},天天下降{:.2f}".format(day_up, day_down))
天天向上6.17,天天下降0.16
天天向上时有双休,如果要达到天天向上的效果,每天应多努力多少? 函数版
def day_UP(df):
day_up = 1.0
for i in range(365):
if i % 7 in [6, 0]:
day_up = day_up * (1 - 0.01)
else:
day_up = day_up * (1 + df)
return day_up
day_factor = 0.01
while (day_UP(day_factor) < 37.78):
day_factor += 0.001
print("每天应努力:{:.3f}".format(day_factor))
每天应努力:0.019
十年之内 地球体重 与 月球体重
# 计算在月球和地球上的体重
weight, Conversion = 50, 0.165
# 一共10年,分别进行计算和打印
for i in range(10):
weight += 0.5
print("{}年后,我在地球的体重为{}KG".format(i + 1, weight))
print("{}年后,我在月球的体重为{:.2f}KG".format(i + 1, weight * Conversion))
1年后,我在地球的体重为50.5KG
1年后,我在月球的体重为8.33KG
2年后,我在地球的体重为51.0KG
2年后,我在月球的体重为8.42KG
3年后,我在地球的体重为51.5KG
3年后,我在月球的体重为8.50KG
4年后,我在地球的体重为52.0KG
4年后,我在月球的体重为8.58KG
5年后,我在地球的体重为52.5KG
5年后,我在月球的体重为8.66KG
6年后,我在地球的体重为53.0KG
6年后,我在月球的体重为8.75KG
7年后,我在地球的体重为53.5KG
7年后,我在月球的体重为8.83KG
8年后,我在地球的体重为54.0KG
8年后,我在月球的体重为8.91KG
9年后,我在地球的体重为54.5KG
9年后,我在月球的体重为8.99KG
10年后,我在地球的体重为55.0KG
10年后,我在月球的体重为9.08KG
字符统计 反向打印 字典实现
numbers = input("请输入一个数字:")
Length = len(numbers)
print(f'这个数字的长度为:{Length}')
请输入一个数字:20
这个数字的长度为:2
为了统计字符个数,引入字典。字典可以通俗理解为:字典图键值对
字典使用{}包起来,也可以用dict()定义
mapping = {}
# 遍历这个输入的字符串并统计字符出现的次数
for number in numbers:
'''
尝试获取key中的值,如果没有对应的Key
则将key设置默认值0,使得条件不成立执行else.
如果获取到了那么说明统计过了,就继续循环
'''
if mapping.get(number, 0) != 0:
continue
'''
走到这里说明上面这个Key在字典中没有
那么将这个key添加到字典中
并统计出现的次数
'''
else:
mapping[number] = numbers.count(number)
'''
for item in mapping.keys():
print(f'{item}出现了{mapping.get(item)}次')
'''
print(mapping)
'''
负数反向输出法,此方法可以不声明Length
for i in range(len(numbers)):
print(numbers[-(i + 1)])
'''
# 正序反向输出法,此方法必须需要声明Length
for i in range(Length):
print(numbers[Length - 1])
Length -= 1
{'2': 1, '0': 1}
0
2
字符统计 反向输出 遍历字符串实现
number = input("请输入数字:")
location = -1
print(f'输入的是{len(number)}位数')
for i in "0123456789":
if number.count(i) != 0:
print(f'{i}出现的次数为:{number.count(i)}')
for i in range(len(number)):
print(number[location])
location -= 1
请输入数字:2365
输入的是4位数
2出现的次数为:1
3出现的次数为:1
5出现的次数为:1
6出现的次数为:1
5
6
3
2
对象数字的工作日日期
week_str = "星期一星期二星期三星期四星期五星期六星期日"
week_id = int(input("请输入星期数字(1-7):"))
pos = (week_id - 1) * 3
print(week_str[pos:pos+3])
请输入星期数字(1-7):7
星期日