设计一个函数, 在桌面上创建10个文本, 以数字给它们命名
def text_creation():
path = r'C:\Users\Black\Desktop\test\\'
for name in range(1, 11):
with open(path + str(name) + '.txt', 'w', encoding='utf-8') as f:
f.write(str(name))
print('Done!')
text_creation()
设计一个复利计算函数 invest(), 它包含三个参数: amount(资金), rate(利率), time(投资时间). 输入每个参数后调用函数, 应该返回每一年的资金总额
def invest(amount, rate, time):
for year in range(1, time + 1):
amount = amount * (1 + rate)
print(f'year {year} : ${amount}')
invest(100, 0.05, 10)
'''
year 1 : $105.0
year 2 : $110.25
year 3 : $115.7625
year 4 : $121.55062500000001
year 5 : $127.62815625000002
year 6 : $134.00956406250003
year 7 : $140.71004226562505
year 8 : $147.74554437890632