我刚开始学习Python,如果能给我一些建议,我将不胜感激。在
当我试图保存“总价”列表中的最后一个总价时,它没有保存。 我做错什么了?在
还有,有没有更好的方法来写这个? 谢谢你
price_list = {'gold': 10,
'red': 20,
'brown': 30
}
vol_list = {'gold': 0.1,
'red': 0.2,
'brown': 0.3
}
cl_list = {'gold': 100,
'red': 200,
'brown': 300
}
while True:
print("What do you want to buy?")
choice = input("Choices: gold, red or brown > ")
amount = int(input("How many do you want? >"))
price = price_list[choice] * amount
vol = (cl_list[choice] * amount) * vol_list[choice]
total_price = []
total_vol = []
total_price.append(price)
total_vol.append(vol)
print_price = sum(total_price[:])
print_vol = sum(total_vol[:])
print("-" * 10)
print("Your total is now: ", print_price)
print("Your total vol is now: ", print_vol)
print("-" * 10)
cont = input("Do you want to continue shopping [Y/n]?> ")
if cont == "n" or cont == "no":
print("#" * 10)
print("#" * 10)
print("Your total is: ", print_price)
print("Your total vol is: ", print_vol)
print("#" * 10)
print("#" * 10)
break
您正在while循环中初始化数组,因此每次新的迭代都会覆盖旧的
total_price
。在在循环之前初始化数组
相关问题 更多 >
编程相关推荐