python3-编程题之商品价格计算器

版权声明:本文为博主原创文章,转载请附上博文链接 https://blog.csdn.net/weixin_44065501/article/details/89391261

一个上商品价格计算器,使用一个数组存储商品的价格,数组中每个元素只存一个数字,价格保留两位小数,不存储小数点。例如A商品的价格为12.00元,在数组中存储为[1,2,0,0]。
现在每个商品涨价33元,请编写一个函数,返回涨价后的价格,仍以数组形式存储。
例如:
函数输入[1,2,0,0](商品原价12.00元)
函数输入[4,5,0,0](商品原价45.00元)

def price(): #定义价格函数
    original_price= float(input("请输入原价格:"))
    present_price_list = [] #现价
    present_price = ("%.2f" % (original_price + 33)) # 把现价转成浮点数,保留两位小数
    for j in range(len(present_price)): # 存储为列表
        if present_price[j] != '.': # 去掉小数点
            present_price_list.append(int(present_price[j])) #把列表的每个元素变成整数
    print(present_price_list)
    return present_price_list
price()

猜你喜欢

转载自blog.csdn.net/weixin_44065501/article/details/89391261