João Vitor Gomes
João Vitor Gomes

Reputation: 363

How come the order of parameters doesn't matter in this function?

Why it seems that in check() function it doesn't matter if I pass the parameters as check(x,y) or check(y,x)?

I tried shifting x and y to see if it would give me a different output

import random

def guess(a, b):
    x = random.randint(a, b)
    return x

def check(a, b):
    if y**2 == x:
        print(x)
        print(y)
        return True
    else:
        return False
    
x = 100
left, right = 0, x

y = guess(left, right)

while not check(x, y): 
    y = guess(left, right)
    
print("answer", y)

Upvotes: 0

Views: 250

Answers (1)

Tomerikoo
Tomerikoo

Reputation: 19414

I think you're making a salad between (a,b) and (x,y).

Your function check uses the "global" (from the outer scope) x and y and not the (x,y) you pass it in the call check(x,y).

Maybe you meant to use a and b instead of x and y inside the definition of check.


You function expects to get 2 arguments as your signature suggests (def check(a, b):) but then nowhere inside it you actually use those arguments. Instead you use x and y. This does not raise an error because Python looks in the outer scopes for those variables and finds them. If you want the order to matter you should change to:

def check(a, b):
    if b**2 == a:
        print(a)
        print(b)
        return True
    else:
        return False

Upvotes: 2

Related Questions