2.14 字符串格式化练习题讲解
一、习题讲解
题目:
定义如下变量:
name:公司名
stock_price:当前股价
stock_code:股票代码
stock_price_daily_growth_factor:股票每日增长系数,浮点数类型,比如1.2
growth_days:增长天数
计算,经过growth_days天的增长后,骨架达到了多少
使用字符串格式化进行输出,如果是浮点数,要求小数点精度2位数
示例输出:
公司:公司名,股票代码:010010,当前股价:12.34
每日增长系数是:1.2,经过6天增长后,股价达到了34.56
答案:
name = "snow" stock_price = 19.99 stock_code = "003032" stock_price_daily_growth_factor = 1.2 growth_days = 7 last_price = stock_price_daily_growth_factor * stock_price ** growth_days print(f"公司:{name},股票代码:{stock_code},当前股价:{stock_price}") print("每日增长系数是:%.2f,经过%d天增长后,股价达到了%.2f" % (stock_price_daily_growth_factor, growth_days, last_price))
Last updated
Was this helpful?