Handling selections for a vending machine











up vote
1
down vote

favorite












To summarise, the code is based of a vending machine and allows the user to choose an item once chosen, it will add 1 to the item to display how many total items the user has.



However, it is incredibly long and more or less it essentially repeats the same code again which is inefficient. How can I compress this code into smaller sections?



if selection == 1:
item = "Chocolate"
print ("1 for Diary Milk 2 for Maltesars 3 for Kit Kat:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Diary Milk"
itemValue = itemValue + 1
elif selection == 2:
item = "Maltesars"
itemValue = itemValue + 1

elif selection == 3:
item = "Kit kat"
itemValue = itemValue + 1

elif selection ==2:
item = "Gum"
print ("1 for Airwaves 2 for Extra Pepermint 3 for Wriggley's Summer fruit:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Airwaves"
itemValue = itemValue+1
elif selection == 2:
item = "Extra Pepermint"
itemValue = itemValue+1

elif selection == 3:
item = "Wriggley's Summer fruit"

elif selection ==3:
item = "Fizzy Drink"
print ("1 for Coke 2 for Tango Cherry 3 for Dr Pepper:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Coke"
itemValue = itemValue+1
elif selection == 2:
item = "Tango Cherry"
itemValue = itemValue+1
elif selection == 3:
item = "Dr Pepper"
itemValue = itemValue+1









share|improve this question
























  • Since each string and numerical output depends on the preceding if statement, it can be rewritten cleaner using dictionaries instead, where each input acts as a key for the desired output values.
    – MPath
    Oct 6 '17 at 10:58















up vote
1
down vote

favorite












To summarise, the code is based of a vending machine and allows the user to choose an item once chosen, it will add 1 to the item to display how many total items the user has.



However, it is incredibly long and more or less it essentially repeats the same code again which is inefficient. How can I compress this code into smaller sections?



if selection == 1:
item = "Chocolate"
print ("1 for Diary Milk 2 for Maltesars 3 for Kit Kat:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Diary Milk"
itemValue = itemValue + 1
elif selection == 2:
item = "Maltesars"
itemValue = itemValue + 1

elif selection == 3:
item = "Kit kat"
itemValue = itemValue + 1

elif selection ==2:
item = "Gum"
print ("1 for Airwaves 2 for Extra Pepermint 3 for Wriggley's Summer fruit:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Airwaves"
itemValue = itemValue+1
elif selection == 2:
item = "Extra Pepermint"
itemValue = itemValue+1

elif selection == 3:
item = "Wriggley's Summer fruit"

elif selection ==3:
item = "Fizzy Drink"
print ("1 for Coke 2 for Tango Cherry 3 for Dr Pepper:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Coke"
itemValue = itemValue+1
elif selection == 2:
item = "Tango Cherry"
itemValue = itemValue+1
elif selection == 3:
item = "Dr Pepper"
itemValue = itemValue+1









share|improve this question
























  • Since each string and numerical output depends on the preceding if statement, it can be rewritten cleaner using dictionaries instead, where each input acts as a key for the desired output values.
    – MPath
    Oct 6 '17 at 10:58













up vote
1
down vote

favorite









up vote
1
down vote

favorite











To summarise, the code is based of a vending machine and allows the user to choose an item once chosen, it will add 1 to the item to display how many total items the user has.



However, it is incredibly long and more or less it essentially repeats the same code again which is inefficient. How can I compress this code into smaller sections?



if selection == 1:
item = "Chocolate"
print ("1 for Diary Milk 2 for Maltesars 3 for Kit Kat:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Diary Milk"
itemValue = itemValue + 1
elif selection == 2:
item = "Maltesars"
itemValue = itemValue + 1

elif selection == 3:
item = "Kit kat"
itemValue = itemValue + 1

elif selection ==2:
item = "Gum"
print ("1 for Airwaves 2 for Extra Pepermint 3 for Wriggley's Summer fruit:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Airwaves"
itemValue = itemValue+1
elif selection == 2:
item = "Extra Pepermint"
itemValue = itemValue+1

elif selection == 3:
item = "Wriggley's Summer fruit"

elif selection ==3:
item = "Fizzy Drink"
print ("1 for Coke 2 for Tango Cherry 3 for Dr Pepper:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Coke"
itemValue = itemValue+1
elif selection == 2:
item = "Tango Cherry"
itemValue = itemValue+1
elif selection == 3:
item = "Dr Pepper"
itemValue = itemValue+1









share|improve this question















To summarise, the code is based of a vending machine and allows the user to choose an item once chosen, it will add 1 to the item to display how many total items the user has.



However, it is incredibly long and more or less it essentially repeats the same code again which is inefficient. How can I compress this code into smaller sections?



if selection == 1:
item = "Chocolate"
print ("1 for Diary Milk 2 for Maltesars 3 for Kit Kat:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Diary Milk"
itemValue = itemValue + 1
elif selection == 2:
item = "Maltesars"
itemValue = itemValue + 1

elif selection == 3:
item = "Kit kat"
itemValue = itemValue + 1

elif selection ==2:
item = "Gum"
print ("1 for Airwaves 2 for Extra Pepermint 3 for Wriggley's Summer fruit:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Airwaves"
itemValue = itemValue+1
elif selection == 2:
item = "Extra Pepermint"
itemValue = itemValue+1

elif selection == 3:
item = "Wriggley's Summer fruit"

elif selection ==3:
item = "Fizzy Drink"
print ("1 for Coke 2 for Tango Cherry 3 for Dr Pepper:")
selection = int(input("Please enter your option:"))
if selection == 1:
item = "Coke"
itemValue = itemValue+1
elif selection == 2:
item = "Tango Cherry"
itemValue = itemValue+1
elif selection == 3:
item = "Dr Pepper"
itemValue = itemValue+1






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 5 '17 at 17:33









200_success

127k15148410




127k15148410










asked Oct 5 '17 at 16:57









Lucian

62




62












  • Since each string and numerical output depends on the preceding if statement, it can be rewritten cleaner using dictionaries instead, where each input acts as a key for the desired output values.
    – MPath
    Oct 6 '17 at 10:58


















  • Since each string and numerical output depends on the preceding if statement, it can be rewritten cleaner using dictionaries instead, where each input acts as a key for the desired output values.
    – MPath
    Oct 6 '17 at 10:58
















Since each string and numerical output depends on the preceding if statement, it can be rewritten cleaner using dictionaries instead, where each input acts as a key for the desired output values.
– MPath
Oct 6 '17 at 10:58




Since each string and numerical output depends on the preceding if statement, it can be rewritten cleaner using dictionaries instead, where each input acts as a key for the desired output values.
– MPath
Oct 6 '17 at 10:58










1 Answer
1






active

oldest

votes

















up vote
2
down vote













The biggest thing you can do is to use string formatting to remove redundancy. Now instead of having to repeatedly type "1 for Coke 2 for Tango Cherry 3 for Dr Pepper:, you factor out the common parts, put the foods in a list, and use that list to populate the string. I also removed your first assignment to item, (ie item = "Gum") for each, because they immediately get removed. Finally, all three circumstances can be combined by indexing into a list of lists.



format_string = "1 for {0} 2 for {1} 3 for {2}:"

items = [["Diary Milk", "Maltesars", "Kit kat"],
["Airwaves","Extra Pepermint","Wriggley's Summer fruit"],
["Coke", "Tango Cherry", "Dr Pepper"]]

print(format_string.format(*items[selection - 1]))
new_selection = int(input("Please enter your option:"))
item = items[selection - 1][new_selection - 1]
itemValue += 1


Taken together, this reduces your code from 50 lines, to 10.






share|improve this answer




















    protected by Simon Forsberg Nov 14 at 11:51



    Thank you for your interest in this question.
    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



    Would you like to answer one of these unanswered questions instead?














    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    The biggest thing you can do is to use string formatting to remove redundancy. Now instead of having to repeatedly type "1 for Coke 2 for Tango Cherry 3 for Dr Pepper:, you factor out the common parts, put the foods in a list, and use that list to populate the string. I also removed your first assignment to item, (ie item = "Gum") for each, because they immediately get removed. Finally, all three circumstances can be combined by indexing into a list of lists.



    format_string = "1 for {0} 2 for {1} 3 for {2}:"

    items = [["Diary Milk", "Maltesars", "Kit kat"],
    ["Airwaves","Extra Pepermint","Wriggley's Summer fruit"],
    ["Coke", "Tango Cherry", "Dr Pepper"]]

    print(format_string.format(*items[selection - 1]))
    new_selection = int(input("Please enter your option:"))
    item = items[selection - 1][new_selection - 1]
    itemValue += 1


    Taken together, this reduces your code from 50 lines, to 10.






    share|improve this answer

























      up vote
      2
      down vote













      The biggest thing you can do is to use string formatting to remove redundancy. Now instead of having to repeatedly type "1 for Coke 2 for Tango Cherry 3 for Dr Pepper:, you factor out the common parts, put the foods in a list, and use that list to populate the string. I also removed your first assignment to item, (ie item = "Gum") for each, because they immediately get removed. Finally, all three circumstances can be combined by indexing into a list of lists.



      format_string = "1 for {0} 2 for {1} 3 for {2}:"

      items = [["Diary Milk", "Maltesars", "Kit kat"],
      ["Airwaves","Extra Pepermint","Wriggley's Summer fruit"],
      ["Coke", "Tango Cherry", "Dr Pepper"]]

      print(format_string.format(*items[selection - 1]))
      new_selection = int(input("Please enter your option:"))
      item = items[selection - 1][new_selection - 1]
      itemValue += 1


      Taken together, this reduces your code from 50 lines, to 10.






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        The biggest thing you can do is to use string formatting to remove redundancy. Now instead of having to repeatedly type "1 for Coke 2 for Tango Cherry 3 for Dr Pepper:, you factor out the common parts, put the foods in a list, and use that list to populate the string. I also removed your first assignment to item, (ie item = "Gum") for each, because they immediately get removed. Finally, all three circumstances can be combined by indexing into a list of lists.



        format_string = "1 for {0} 2 for {1} 3 for {2}:"

        items = [["Diary Milk", "Maltesars", "Kit kat"],
        ["Airwaves","Extra Pepermint","Wriggley's Summer fruit"],
        ["Coke", "Tango Cherry", "Dr Pepper"]]

        print(format_string.format(*items[selection - 1]))
        new_selection = int(input("Please enter your option:"))
        item = items[selection - 1][new_selection - 1]
        itemValue += 1


        Taken together, this reduces your code from 50 lines, to 10.






        share|improve this answer












        The biggest thing you can do is to use string formatting to remove redundancy. Now instead of having to repeatedly type "1 for Coke 2 for Tango Cherry 3 for Dr Pepper:, you factor out the common parts, put the foods in a list, and use that list to populate the string. I also removed your first assignment to item, (ie item = "Gum") for each, because they immediately get removed. Finally, all three circumstances can be combined by indexing into a list of lists.



        format_string = "1 for {0} 2 for {1} 3 for {2}:"

        items = [["Diary Milk", "Maltesars", "Kit kat"],
        ["Airwaves","Extra Pepermint","Wriggley's Summer fruit"],
        ["Coke", "Tango Cherry", "Dr Pepper"]]

        print(format_string.format(*items[selection - 1]))
        new_selection = int(input("Please enter your option:"))
        item = items[selection - 1][new_selection - 1]
        itemValue += 1


        Taken together, this reduces your code from 50 lines, to 10.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Oct 5 '17 at 19:44









        Oscar Smith

        2,678922




        2,678922

















            protected by Simon Forsberg Nov 14 at 11:51



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?



            Popular posts from this blog

            Morgemoulin

            Scott Moir

            Souastre