keeping a running total of the values in a dictionary










-1















I want the user to pick an item from the shop and I want to use a loop to keep track of the prices(the value in the dictionary) to add up and keep a running total after every input from user. If the user inputs something that is not in the dictionary, it should say that it does not exist.
thanks in advance for help



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)









share|improve this question






















  • By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC-BY-SA 3.0 license). By SE policy, any vandalism will be reverted and subsequent attempts will get you banned. If you would like to disassociate this post from your account, see What is the proper route for a disassociation request?

    – Samuel Liew
    Nov 14 '18 at 1:16















-1















I want the user to pick an item from the shop and I want to use a loop to keep track of the prices(the value in the dictionary) to add up and keep a running total after every input from user. If the user inputs something that is not in the dictionary, it should say that it does not exist.
thanks in advance for help



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)









share|improve this question






















  • By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC-BY-SA 3.0 license). By SE policy, any vandalism will be reverted and subsequent attempts will get you banned. If you would like to disassociate this post from your account, see What is the proper route for a disassociation request?

    – Samuel Liew
    Nov 14 '18 at 1:16













-1












-1








-1








I want the user to pick an item from the shop and I want to use a loop to keep track of the prices(the value in the dictionary) to add up and keep a running total after every input from user. If the user inputs something that is not in the dictionary, it should say that it does not exist.
thanks in advance for help



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)









share|improve this question














I want the user to pick an item from the shop and I want to use a loop to keep track of the prices(the value in the dictionary) to add up and keep a running total after every input from user. If the user inputs something that is not in the dictionary, it should say that it does not exist.
thanks in advance for help



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)






python loops dictionary while-loop running-total






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 2:51









SahandkeSahandke

1




1












  • By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC-BY-SA 3.0 license). By SE policy, any vandalism will be reverted and subsequent attempts will get you banned. If you would like to disassociate this post from your account, see What is the proper route for a disassociation request?

    – Samuel Liew
    Nov 14 '18 at 1:16

















  • By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC-BY-SA 3.0 license). By SE policy, any vandalism will be reverted and subsequent attempts will get you banned. If you would like to disassociate this post from your account, see What is the proper route for a disassociation request?

    – Samuel Liew
    Nov 14 '18 at 1:16
















By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC-BY-SA 3.0 license). By SE policy, any vandalism will be reverted and subsequent attempts will get you banned. If you would like to disassociate this post from your account, see What is the proper route for a disassociation request?

– Samuel Liew
Nov 14 '18 at 1:16





By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the CC-BY-SA 3.0 license). By SE policy, any vandalism will be reverted and subsequent attempts will get you banned. If you would like to disassociate this post from your account, see What is the proper route for a disassociation request?

– Samuel Liew
Nov 14 '18 at 1:16












1 Answer
1






active

oldest

votes


















1














You should update the value of total every time a choice is made. See example below



def main():
machine= "1.shirt: $":10, "2.pants: $":15, "3.sweater: $":20,"4.socks: $":5, "5.hat: $":7
total=0
for key, value in machine.items():
print(key,value)
print("------------------------------")
while True: # Keep Looping
selection = input("Please choose the number of the item you would like to purchase: ")

if selection=="1":
total += machine["1.shirt: $"];
print("You chose shirt and your total is: $", total)
elif selection=="2":
total += machine["2.pants: $"];
print("You chose shirt and your total is: $", total)
else:
print("Your option does not exist. Your total is: ",total)
break





share|improve this answer

























  • heh, you copied the copy-paste error

    – John La Rooy
    Nov 13 '18 at 2:57











  • thank you so much

    – Sahandke
    Nov 13 '18 at 3:13










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273080%2fkeeping-a-running-total-of-the-values-in-a-dictionary%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You should update the value of total every time a choice is made. See example below



def main():
machine= "1.shirt: $":10, "2.pants: $":15, "3.sweater: $":20,"4.socks: $":5, "5.hat: $":7
total=0
for key, value in machine.items():
print(key,value)
print("------------------------------")
while True: # Keep Looping
selection = input("Please choose the number of the item you would like to purchase: ")

if selection=="1":
total += machine["1.shirt: $"];
print("You chose shirt and your total is: $", total)
elif selection=="2":
total += machine["2.pants: $"];
print("You chose shirt and your total is: $", total)
else:
print("Your option does not exist. Your total is: ",total)
break





share|improve this answer

























  • heh, you copied the copy-paste error

    – John La Rooy
    Nov 13 '18 at 2:57











  • thank you so much

    – Sahandke
    Nov 13 '18 at 3:13















1














You should update the value of total every time a choice is made. See example below



def main():
machine= "1.shirt: $":10, "2.pants: $":15, "3.sweater: $":20,"4.socks: $":5, "5.hat: $":7
total=0
for key, value in machine.items():
print(key,value)
print("------------------------------")
while True: # Keep Looping
selection = input("Please choose the number of the item you would like to purchase: ")

if selection=="1":
total += machine["1.shirt: $"];
print("You chose shirt and your total is: $", total)
elif selection=="2":
total += machine["2.pants: $"];
print("You chose shirt and your total is: $", total)
else:
print("Your option does not exist. Your total is: ",total)
break





share|improve this answer

























  • heh, you copied the copy-paste error

    – John La Rooy
    Nov 13 '18 at 2:57











  • thank you so much

    – Sahandke
    Nov 13 '18 at 3:13













1












1








1







You should update the value of total every time a choice is made. See example below



def main():
machine= "1.shirt: $":10, "2.pants: $":15, "3.sweater: $":20,"4.socks: $":5, "5.hat: $":7
total=0
for key, value in machine.items():
print(key,value)
print("------------------------------")
while True: # Keep Looping
selection = input("Please choose the number of the item you would like to purchase: ")

if selection=="1":
total += machine["1.shirt: $"];
print("You chose shirt and your total is: $", total)
elif selection=="2":
total += machine["2.pants: $"];
print("You chose shirt and your total is: $", total)
else:
print("Your option does not exist. Your total is: ",total)
break





share|improve this answer















You should update the value of total every time a choice is made. See example below



def main():
machine= "1.shirt: $":10, "2.pants: $":15, "3.sweater: $":20,"4.socks: $":5, "5.hat: $":7
total=0
for key, value in machine.items():
print(key,value)
print("------------------------------")
while True: # Keep Looping
selection = input("Please choose the number of the item you would like to purchase: ")

if selection=="1":
total += machine["1.shirt: $"];
print("You chose shirt and your total is: $", total)
elif selection=="2":
total += machine["2.pants: $"];
print("You chose shirt and your total is: $", total)
else:
print("Your option does not exist. Your total is: ",total)
break






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 2:58

























answered Nov 13 '18 at 2:57









BishalBishal

556216




556216












  • heh, you copied the copy-paste error

    – John La Rooy
    Nov 13 '18 at 2:57











  • thank you so much

    – Sahandke
    Nov 13 '18 at 3:13

















  • heh, you copied the copy-paste error

    – John La Rooy
    Nov 13 '18 at 2:57











  • thank you so much

    – Sahandke
    Nov 13 '18 at 3:13
















heh, you copied the copy-paste error

– John La Rooy
Nov 13 '18 at 2:57





heh, you copied the copy-paste error

– John La Rooy
Nov 13 '18 at 2:57













thank you so much

– Sahandke
Nov 13 '18 at 3:13





thank you so much

– Sahandke
Nov 13 '18 at 3:13

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273080%2fkeeping-a-running-total-of-the-values-in-a-dictionary%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20