lundi 3 juin 2019

How to write a two variable function with an IF statement

I am trying to write a two variable equation that evaluates a hypothetical speed and boolean operator that reduces the speed by 5 if true. Depending on the speed and boolean operator the equation should print 'No Ticket', 'Small Ticket' or 'Big Ticket'.

I first tried writing this as a lambda expression but found out that lambda expressions must be a single line. I then wrote this as a normal expression, with the single x variable and it worked perfectly. After I added the Y variable, the function stopped working. I don't know if I am defining x wrong, if I cannot do nested IFs like I am trying, or if this needs to be two functions. Any help on this would be greatly appreciated! :)

def caught_speeding(x,y):
    if y == 1:
        x = x - 5
    else:
        x = x

        if x <= 60:
                return print('No Ticket')
        elif x > 60 or x <=80:
                return print('Small Ticket')
        elif x >= 81:
                return print('Big Ticket')

caught_speeding(100,0)

I am not getting any error messages. If I set y = 1 it always returns 'Small Ticket', if I set y = 0 then it does not return anything. :/

Aucun commentaire:

Enregistrer un commentaire