我希望用户从商店中挑选一件商品,我希望使用一个循环来跟踪价格(字典中的值),在用户每次输入之后进行累加并保持一个运行总数。如果用户输入了字典中没有的内容,应该说它不存在。 提前谢谢你的帮助
def main():
machine= {"1.shirt: $":10, "2.pants: $":15, "3.sweater: $":20,"4.socks: $":5, "5.hat: $":7}
for key, value in machine.items():
print(key,value)
print("------------------------------")
selection = input("Please choose the number of the item you would like to purchase: ")
total=0
for i in machine:
if selection=="1":
print("You chose shirt and your total is: $", machine["1.shirt: $"])
elif selection=="2":
print("You chose shirt and your total is: $", machine["2.pants: $"])
elif selection=="3":
print("You chose shirt and your total is: $", machine["3.sweater: $"])
elif selection=="4":
print("You chose shirt and your total is: $", machine["4.socks: $"])
elif selection=="5":
print("You chose shirt and your total is: $", machine["5.hat: $"])
else:
print("Your option does not exist. Your total is: ",total)
每次做出选择时,都应该更新total的值。参见下面的示例
相关问题 更多 >
编程相关推荐