The pgcd
variable inside of your couplepgcd
function is local to that function, so when you call it even if pgcd
is changed to True
and returned from the function, this does not change the value of the pgcd
variable that you are using in the loop condition.
You are also unnecessarily passing in the current value of pgcd
into the function, this isn't used anywhere so it isn't necessary.
Here are all of the options you can use to fix this:
Assign the return value of the function to the variable
pgcd
:pgcd = False while pgcd == False: [...] pgcd = couplepgcd()
Use the
global
statement within the function so that assignment topgcd
within your function overwrites the global variable (this only works if youwhile
loop is in the global scope, or on Python 3.x you could use thenonlocal
keyword):def couplepgcd(): global pgcd try: assert gcd(a,26) == 1 except Exception: print("a et 26 doivent être premiers entre eux !")
Use a mutable variable that you modify within the function, for example with a one-element list:
def couplepgcd(pgcd): try: assert gcd(a,26) == 1 pgcd[0] = True except Exception: print("a et 26 doivent être premiers entre eux !") pgcd = [False] while not pgcd[0]: [...] couplepgcd(pgcd)