Programming question using loops

edited March 2017 in Python Mode

Hello, I'm attempting to do this one assignment my professor assigned, but I'm having some trouble. Here is my full code:

change = 0 #calculates the change

#Y coordinates for each coin
y1 = 60
y2 = 60
y3 = 60 
y4 = 60
y5 = 60
y6 = 60
def setup():
    size(600,600)

def draw():
    global y1,y2,y3,y4,y5,y6

    #Draws the coins on canvas
    fill(150)
    ellipse(60,y1,90,90)

    fill(230,225,50)
    ellipse(160,y2,90,90)

    fill(150)
    ellipse(260,y3,70,70)

    fill(150)
    ellipse(360,y4,60,60)

    fill(150)
    ellipse(460,y5,50,50)

    fill(200,145,30)
    ellipse(560,y6,40,40)

    #Keypressed increases and decreases number of change    
def keyPressed():
    global change,y1,y2,y3,y4,y5,y6
    if key == 'q':
        change = change + 1.00
        if (((change*(change+2))/4) == change):
            y2 = y2+20
        else:
            y1 = y1+20
    if key == 'a':
        change = change - 1.00
    if key == 'w':
        change = change + 0.10
    if key == 's':
        change = change - 0.10
    if key == 'e':
        change = change + 0.01
    if key == 'd':
        change = change - 0.01

    print(change)

But the snippet I need help with is this:

def keyPressed():
        global change,y1,y2,y3,y4,y5,y6
        if key == 'q':
            change = change + 1.00
            if (((change*(change+2))/4) == change):
                y2 = y2+60
            else:
                y1 = y1+60

So basically what I'm trying to do is, when the key Q is pressed it increases the variable change. I want it so every time the variable change lands on an even number, the Y coordinate for one of ellipse to increase, thus displaying a new ellipse under that one. Else, if it's on an odd number, I want the Y coordinate for a DIFFERENT ellipse to increase. However, that's not happening and I can't figure out why. Only the first ellipse's y coordinate increases even if the variable change is an odd number and the second ellipse's y coordinate only increases when change is 2, which doesn't make sense because that's an even number and should only increase when it's an odd number.

My prof suggested I use loops and not if statements but I dunno how to do that.

Thanks, sorry if I'm confusing

Answers

  • change = change + 1.00

    Why the float 1.00 increment? Can't you just use integer literals? change += 1 :-@

    http://py.Processing.org/reference/float.html
    http://py.Processing.org/reference/int.html

  • My assignment requires me to have the user add/subtract coins on canvas and calculate the total change. I'm Canadian so pressing the key q adds loonies which one loonie is 100 cents. I thought it'd be better to have it as 1.00 since later on, I have to incorporate dimes(0.10) and nickels(0.5). But I'm not sure if that's the best way to do it.

  • You can always divide change by 100 for displaying purposes.
    For more precision, keep change as an integer, so you can use operator == safely.

  • So instead of change += 0.5, go w/ change += 50 instead. *-:)

  • Okay thank you, I don't know why I didn't think of that :/. I'm still having trouble making my if statement under key == 'q' work :(

  • edited March 2017 Answer ✓
    def keyPressed():
        global change, y1, y2, y3, y4, y5, y6
    
        k = chr(keyCode) if key != CODED else keyCode
    
        if k == 'Q':
            change += 100
            if change/100 & 1: y2 += 60
            else:              y1 += 60
    
  • Thank you so much for your help

Sign In or Register to comment.