0

I am trying to do a while loop that execute a function while the verification is not changed. my code looks like this:

from time import sleep

verif = 0
num = 5

def doso(num, verif):
    if num%11 == 0:
        verif += 1

    elif num%14 == 0:
        verif += 1

    print(num)
    return verif

while verif == 0:
    doso(num, verif)
    num += 1
    sleep(1)

so for now it run infinitely... i would like if it stoped when it find a multiple of 11 or 14

**its an example

Arco Bast
  • 3,595
  • 2
  • 26
  • 53
oat lion
  • 31
  • 5
  • 3
    because the value of `verif` is not changed inside the loop. You probably meant to do `verif = doso(num, verif)` – Ma0 Mar 17 '20 at 16:14
  • `verif += 1` inside the `doso` function creates a local variable, but does not change the global variable defined above. – mkrieger1 Mar 17 '20 at 16:15
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – KJTHoward Mar 17 '20 at 16:15

2 Answers2

2

Try updating the variable:

while verif == 0:
    verif = doso(num, verif)
    num += 1
    sleep(1)
Arco Bast
  • 3,595
  • 2
  • 26
  • 53
2

To avoid running into an XY problem, note that you do not need verif at all. You can simply do:

num = 5

while True:
    if num%11 == 0 or num%14 == 0:
      break
    else:
      num += 1
Ma0
  • 15,057
  • 4
  • 35
  • 65