How to add total number of items using list/conditional statments












0















trying to write a code where when a certain key is pressed, the word pops up on the canvas (in this case they are all drinks). When the user keeps pressing the certain keys to add a drink, the total cost adds up.



However, the only thing in my code the works is the word "water" and the total cost keeps overlaying.



How do I fix my code so that each menu item appears on the canvas and how to fix the total cost?



(I attempted to move the background colour around however it still did not work. Also this coding is from processing, and it is not java script)



def setup():
size (500, 500)
background (255)

menu = ["Mocha","Coffee","Juice","water"]
prices = [5,2,3,1]
order =
total = ''
cost = 0

Mocha = 5
Coffee = 2
Juice = 3
Water = 1
undo = -1
space = 0
i = 0
str1= 1

def draw():
global total
space = 100
fill (0)
line(250,0,250,500)
textSize (30)
text("Menu", 60, 50)

textSize (15)
for i in range(len(menu)):
str1 = menu[i] + " : $" + str(prices[i])
text(str1, 70, space)
space = space+20

textSize (30)
text( "Your Order:", 260,50)
textSize (15)
#total = text("Cost : $0", 300,450)
total= text(str1, 350, 450)

def keyPressed():
global space
global cost, total, menu, str1, i
if key == 'm' :
i=0
elif key == 'c' :
i=1
elif key == 'j' :
i=2
elif key == 'w' :
i=3
elif undo == 'u' :
i=-1
space = space+20

if i == -1 :
if len(order) > 0:
menu = order.pop(0)
menu.pop(item)
for i in range(len(menu)):
if menu[i] == str1 :
cost = cost - prices[i]
menu.pop(total)
str1 = "Cost : $" + str(cost)
total= text(str1, 350, 450)

else :
order.append(text( menu[i], 300, space+90))
cost = cost+prices[i]
str1 = "Cost : $" + str(cost)
total = text( str1, 360, 450)









share|improve this question

























  • The key which was pressed, when the keyPressed function is called, is contained in the system variable key. You have to compare the variable key to the character 'm', 'c', 'j', 'w' and 'u'.

    – Rabbid76
    Nov 22 '18 at 20:03











  • okay so I just changed it to be if key =="j" but it still does not work

    – Vex143
    Nov 22 '18 at 20:59


















0















trying to write a code where when a certain key is pressed, the word pops up on the canvas (in this case they are all drinks). When the user keeps pressing the certain keys to add a drink, the total cost adds up.



However, the only thing in my code the works is the word "water" and the total cost keeps overlaying.



How do I fix my code so that each menu item appears on the canvas and how to fix the total cost?



(I attempted to move the background colour around however it still did not work. Also this coding is from processing, and it is not java script)



def setup():
size (500, 500)
background (255)

menu = ["Mocha","Coffee","Juice","water"]
prices = [5,2,3,1]
order =
total = ''
cost = 0

Mocha = 5
Coffee = 2
Juice = 3
Water = 1
undo = -1
space = 0
i = 0
str1= 1

def draw():
global total
space = 100
fill (0)
line(250,0,250,500)
textSize (30)
text("Menu", 60, 50)

textSize (15)
for i in range(len(menu)):
str1 = menu[i] + " : $" + str(prices[i])
text(str1, 70, space)
space = space+20

textSize (30)
text( "Your Order:", 260,50)
textSize (15)
#total = text("Cost : $0", 300,450)
total= text(str1, 350, 450)

def keyPressed():
global space
global cost, total, menu, str1, i
if key == 'm' :
i=0
elif key == 'c' :
i=1
elif key == 'j' :
i=2
elif key == 'w' :
i=3
elif undo == 'u' :
i=-1
space = space+20

if i == -1 :
if len(order) > 0:
menu = order.pop(0)
menu.pop(item)
for i in range(len(menu)):
if menu[i] == str1 :
cost = cost - prices[i]
menu.pop(total)
str1 = "Cost : $" + str(cost)
total= text(str1, 350, 450)

else :
order.append(text( menu[i], 300, space+90))
cost = cost+prices[i]
str1 = "Cost : $" + str(cost)
total = text( str1, 360, 450)









share|improve this question

























  • The key which was pressed, when the keyPressed function is called, is contained in the system variable key. You have to compare the variable key to the character 'm', 'c', 'j', 'w' and 'u'.

    – Rabbid76
    Nov 22 '18 at 20:03











  • okay so I just changed it to be if key =="j" but it still does not work

    – Vex143
    Nov 22 '18 at 20:59
















0












0








0








trying to write a code where when a certain key is pressed, the word pops up on the canvas (in this case they are all drinks). When the user keeps pressing the certain keys to add a drink, the total cost adds up.



However, the only thing in my code the works is the word "water" and the total cost keeps overlaying.



How do I fix my code so that each menu item appears on the canvas and how to fix the total cost?



(I attempted to move the background colour around however it still did not work. Also this coding is from processing, and it is not java script)



def setup():
size (500, 500)
background (255)

menu = ["Mocha","Coffee","Juice","water"]
prices = [5,2,3,1]
order =
total = ''
cost = 0

Mocha = 5
Coffee = 2
Juice = 3
Water = 1
undo = -1
space = 0
i = 0
str1= 1

def draw():
global total
space = 100
fill (0)
line(250,0,250,500)
textSize (30)
text("Menu", 60, 50)

textSize (15)
for i in range(len(menu)):
str1 = menu[i] + " : $" + str(prices[i])
text(str1, 70, space)
space = space+20

textSize (30)
text( "Your Order:", 260,50)
textSize (15)
#total = text("Cost : $0", 300,450)
total= text(str1, 350, 450)

def keyPressed():
global space
global cost, total, menu, str1, i
if key == 'm' :
i=0
elif key == 'c' :
i=1
elif key == 'j' :
i=2
elif key == 'w' :
i=3
elif undo == 'u' :
i=-1
space = space+20

if i == -1 :
if len(order) > 0:
menu = order.pop(0)
menu.pop(item)
for i in range(len(menu)):
if menu[i] == str1 :
cost = cost - prices[i]
menu.pop(total)
str1 = "Cost : $" + str(cost)
total= text(str1, 350, 450)

else :
order.append(text( menu[i], 300, space+90))
cost = cost+prices[i]
str1 = "Cost : $" + str(cost)
total = text( str1, 360, 450)









share|improve this question
















trying to write a code where when a certain key is pressed, the word pops up on the canvas (in this case they are all drinks). When the user keeps pressing the certain keys to add a drink, the total cost adds up.



However, the only thing in my code the works is the word "water" and the total cost keeps overlaying.



How do I fix my code so that each menu item appears on the canvas and how to fix the total cost?



(I attempted to move the background colour around however it still did not work. Also this coding is from processing, and it is not java script)



def setup():
size (500, 500)
background (255)

menu = ["Mocha","Coffee","Juice","water"]
prices = [5,2,3,1]
order =
total = ''
cost = 0

Mocha = 5
Coffee = 2
Juice = 3
Water = 1
undo = -1
space = 0
i = 0
str1= 1

def draw():
global total
space = 100
fill (0)
line(250,0,250,500)
textSize (30)
text("Menu", 60, 50)

textSize (15)
for i in range(len(menu)):
str1 = menu[i] + " : $" + str(prices[i])
text(str1, 70, space)
space = space+20

textSize (30)
text( "Your Order:", 260,50)
textSize (15)
#total = text("Cost : $0", 300,450)
total= text(str1, 350, 450)

def keyPressed():
global space
global cost, total, menu, str1, i
if key == 'm' :
i=0
elif key == 'c' :
i=1
elif key == 'j' :
i=2
elif key == 'w' :
i=3
elif undo == 'u' :
i=-1
space = space+20

if i == -1 :
if len(order) > 0:
menu = order.pop(0)
menu.pop(item)
for i in range(len(menu)):
if menu[i] == str1 :
cost = cost - prices[i]
menu.pop(total)
str1 = "Cost : $" + str(cost)
total= text(str1, 350, 450)

else :
order.append(text( menu[i], 300, space+90))
cost = cost+prices[i]
str1 = "Cost : $" + str(cost)
total = text( str1, 360, 450)






python processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 21:00







Vex143

















asked Nov 22 '18 at 19:38









Vex143Vex143

62




62













  • The key which was pressed, when the keyPressed function is called, is contained in the system variable key. You have to compare the variable key to the character 'm', 'c', 'j', 'w' and 'u'.

    – Rabbid76
    Nov 22 '18 at 20:03











  • okay so I just changed it to be if key =="j" but it still does not work

    – Vex143
    Nov 22 '18 at 20:59





















  • The key which was pressed, when the keyPressed function is called, is contained in the system variable key. You have to compare the variable key to the character 'm', 'c', 'j', 'w' and 'u'.

    – Rabbid76
    Nov 22 '18 at 20:03











  • okay so I just changed it to be if key =="j" but it still does not work

    – Vex143
    Nov 22 '18 at 20:59



















The key which was pressed, when the keyPressed function is called, is contained in the system variable key. You have to compare the variable key to the character 'm', 'c', 'j', 'w' and 'u'.

– Rabbid76
Nov 22 '18 at 20:03





The key which was pressed, when the keyPressed function is called, is contained in the system variable key. You have to compare the variable key to the character 'm', 'c', 'j', 'w' and 'u'.

– Rabbid76
Nov 22 '18 at 20:03













okay so I just changed it to be if key =="j" but it still does not work

– Vex143
Nov 22 '18 at 20:59







okay so I just changed it to be if key =="j" but it still does not work

– Vex143
Nov 22 '18 at 20:59














1 Answer
1






active

oldest

votes


















0














The total cost keeps overlaying itself because the stage is not being cleared between draw() loops. Add background(255) at the top of the draw loop so that the stage is refreshed. Doing this however will introduce the need to loop through the order and draw its contents in the "Your Order:" section each loop.



You're getting name clashes with the variable i, which is making your code perform oddly. Remove the i = 0 declaration near the top of your program. This fixes the "water"-only issue.



This needs to be changed: order.append(text( menu[i], 300, space+90)). You can't add Processing draw (text()) methods to an array like this.



Putting this all together (and a few more things) gives us this:



def setup():
size (500, 500)
background (255)

menu = ["Mocha","Coffee","Juice","Water"]
prices = [5,2,3,1]
order =
cost = 0

def draw():
background(255)

fill(0)
line(250,0,250,500)
textSize (30)
text("Menu", 60, 50)

space = 100
textSize (15)
for i in range(len(menu)):
str1 = menu[i] + " : $" + str(prices[i])
text(str1, 70, space)
space = space+20

textSize (30)
text( "Your Order:", 260,50)

textSize (15)
text("Cost : $" + str(cost), 350, 450)

for ypos, item in enumerate(order):
text(menu[item] + ".... $" + str(prices[item]), 270, ypos*20 + 80)

def keyPressed():
global cost, menu
if key == 'm' :
i=0
elif key == 'c' :
i=1
elif key == 'j' :
i=2
elif key == 'w' :
i=3
elif key == 'u' :
i=-1
else: # do nothing if the key is undefined
return

if i == -1 :
if len(order) > 0:
cost -= prices[order[-1]] # subtract cost
order.pop() # pop the order, not the menu!
else :
cost += prices[i]
order.append(i)





share|improve this answer























    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%2f53437195%2fhow-to-add-total-number-of-items-using-list-conditional-statments%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









    0














    The total cost keeps overlaying itself because the stage is not being cleared between draw() loops. Add background(255) at the top of the draw loop so that the stage is refreshed. Doing this however will introduce the need to loop through the order and draw its contents in the "Your Order:" section each loop.



    You're getting name clashes with the variable i, which is making your code perform oddly. Remove the i = 0 declaration near the top of your program. This fixes the "water"-only issue.



    This needs to be changed: order.append(text( menu[i], 300, space+90)). You can't add Processing draw (text()) methods to an array like this.



    Putting this all together (and a few more things) gives us this:



    def setup():
    size (500, 500)
    background (255)

    menu = ["Mocha","Coffee","Juice","Water"]
    prices = [5,2,3,1]
    order =
    cost = 0

    def draw():
    background(255)

    fill(0)
    line(250,0,250,500)
    textSize (30)
    text("Menu", 60, 50)

    space = 100
    textSize (15)
    for i in range(len(menu)):
    str1 = menu[i] + " : $" + str(prices[i])
    text(str1, 70, space)
    space = space+20

    textSize (30)
    text( "Your Order:", 260,50)

    textSize (15)
    text("Cost : $" + str(cost), 350, 450)

    for ypos, item in enumerate(order):
    text(menu[item] + ".... $" + str(prices[item]), 270, ypos*20 + 80)

    def keyPressed():
    global cost, menu
    if key == 'm' :
    i=0
    elif key == 'c' :
    i=1
    elif key == 'j' :
    i=2
    elif key == 'w' :
    i=3
    elif key == 'u' :
    i=-1
    else: # do nothing if the key is undefined
    return

    if i == -1 :
    if len(order) > 0:
    cost -= prices[order[-1]] # subtract cost
    order.pop() # pop the order, not the menu!
    else :
    cost += prices[i]
    order.append(i)





    share|improve this answer




























      0














      The total cost keeps overlaying itself because the stage is not being cleared between draw() loops. Add background(255) at the top of the draw loop so that the stage is refreshed. Doing this however will introduce the need to loop through the order and draw its contents in the "Your Order:" section each loop.



      You're getting name clashes with the variable i, which is making your code perform oddly. Remove the i = 0 declaration near the top of your program. This fixes the "water"-only issue.



      This needs to be changed: order.append(text( menu[i], 300, space+90)). You can't add Processing draw (text()) methods to an array like this.



      Putting this all together (and a few more things) gives us this:



      def setup():
      size (500, 500)
      background (255)

      menu = ["Mocha","Coffee","Juice","Water"]
      prices = [5,2,3,1]
      order =
      cost = 0

      def draw():
      background(255)

      fill(0)
      line(250,0,250,500)
      textSize (30)
      text("Menu", 60, 50)

      space = 100
      textSize (15)
      for i in range(len(menu)):
      str1 = menu[i] + " : $" + str(prices[i])
      text(str1, 70, space)
      space = space+20

      textSize (30)
      text( "Your Order:", 260,50)

      textSize (15)
      text("Cost : $" + str(cost), 350, 450)

      for ypos, item in enumerate(order):
      text(menu[item] + ".... $" + str(prices[item]), 270, ypos*20 + 80)

      def keyPressed():
      global cost, menu
      if key == 'm' :
      i=0
      elif key == 'c' :
      i=1
      elif key == 'j' :
      i=2
      elif key == 'w' :
      i=3
      elif key == 'u' :
      i=-1
      else: # do nothing if the key is undefined
      return

      if i == -1 :
      if len(order) > 0:
      cost -= prices[order[-1]] # subtract cost
      order.pop() # pop the order, not the menu!
      else :
      cost += prices[i]
      order.append(i)





      share|improve this answer


























        0












        0








        0







        The total cost keeps overlaying itself because the stage is not being cleared between draw() loops. Add background(255) at the top of the draw loop so that the stage is refreshed. Doing this however will introduce the need to loop through the order and draw its contents in the "Your Order:" section each loop.



        You're getting name clashes with the variable i, which is making your code perform oddly. Remove the i = 0 declaration near the top of your program. This fixes the "water"-only issue.



        This needs to be changed: order.append(text( menu[i], 300, space+90)). You can't add Processing draw (text()) methods to an array like this.



        Putting this all together (and a few more things) gives us this:



        def setup():
        size (500, 500)
        background (255)

        menu = ["Mocha","Coffee","Juice","Water"]
        prices = [5,2,3,1]
        order =
        cost = 0

        def draw():
        background(255)

        fill(0)
        line(250,0,250,500)
        textSize (30)
        text("Menu", 60, 50)

        space = 100
        textSize (15)
        for i in range(len(menu)):
        str1 = menu[i] + " : $" + str(prices[i])
        text(str1, 70, space)
        space = space+20

        textSize (30)
        text( "Your Order:", 260,50)

        textSize (15)
        text("Cost : $" + str(cost), 350, 450)

        for ypos, item in enumerate(order):
        text(menu[item] + ".... $" + str(prices[item]), 270, ypos*20 + 80)

        def keyPressed():
        global cost, menu
        if key == 'm' :
        i=0
        elif key == 'c' :
        i=1
        elif key == 'j' :
        i=2
        elif key == 'w' :
        i=3
        elif key == 'u' :
        i=-1
        else: # do nothing if the key is undefined
        return

        if i == -1 :
        if len(order) > 0:
        cost -= prices[order[-1]] # subtract cost
        order.pop() # pop the order, not the menu!
        else :
        cost += prices[i]
        order.append(i)





        share|improve this answer













        The total cost keeps overlaying itself because the stage is not being cleared between draw() loops. Add background(255) at the top of the draw loop so that the stage is refreshed. Doing this however will introduce the need to loop through the order and draw its contents in the "Your Order:" section each loop.



        You're getting name clashes with the variable i, which is making your code perform oddly. Remove the i = 0 declaration near the top of your program. This fixes the "water"-only issue.



        This needs to be changed: order.append(text( menu[i], 300, space+90)). You can't add Processing draw (text()) methods to an array like this.



        Putting this all together (and a few more things) gives us this:



        def setup():
        size (500, 500)
        background (255)

        menu = ["Mocha","Coffee","Juice","Water"]
        prices = [5,2,3,1]
        order =
        cost = 0

        def draw():
        background(255)

        fill(0)
        line(250,0,250,500)
        textSize (30)
        text("Menu", 60, 50)

        space = 100
        textSize (15)
        for i in range(len(menu)):
        str1 = menu[i] + " : $" + str(prices[i])
        text(str1, 70, space)
        space = space+20

        textSize (30)
        text( "Your Order:", 260,50)

        textSize (15)
        text("Cost : $" + str(cost), 350, 450)

        for ypos, item in enumerate(order):
        text(menu[item] + ".... $" + str(prices[item]), 270, ypos*20 + 80)

        def keyPressed():
        global cost, menu
        if key == 'm' :
        i=0
        elif key == 'c' :
        i=1
        elif key == 'j' :
        i=2
        elif key == 'w' :
        i=3
        elif key == 'u' :
        i=-1
        else: # do nothing if the key is undefined
        return

        if i == -1 :
        if len(order) > 0:
        cost -= prices[order[-1]] # subtract cost
        order.pop() # pop the order, not the menu!
        else :
        cost += prices[i]
        order.append(i)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 15:25









        micyclemicycle

        649115




        649115






























            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%2f53437195%2fhow-to-add-total-number-of-items-using-list-conditional-statments%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'