-4
def dinner_completed():
        counter=0
        if counter==0:
            k=False
        if counter==1:
            k=True
        counter+=1
        print('counter',counter)
        return k
    
while not dinner_completed():
        print('1')

Each time I check the function in a while loop, counter is getting initialized to 0 . I don't want to use global variables and my function have to return False only once and True the rest of times.

  • It would help to know *why* you do not want a global variable. The usecase seems to match it exactly. What behavior of global variables do you want to avoid? Do you want to avoid just the global keyword, or similar constructs such as mutable globals or class attributes? – MisterMiyagi Oct 02 '20 at 08:05
  • 1
    For this particular example you could use a generetor function. – Heike Oct 02 '20 at 08:05
  • I want to print '1' in the console only once instead infinite loop is running – Anudeep Kosuri Oct 02 '20 at 08:11
  • @KenY-N I dont want to change the function name as foo – Anudeep Kosuri Oct 02 '20 at 08:17

3 Answers3

1

As suggested in the comments, you can use a generator to avoid the usage of a global variable. You need this, because as you pointed out, the counter variable is set to 0 every time you call dinner_completed(). Here is an example of using generators:

def dinner_completed():
  yield False
  while True:
     yield True

check = dinner_completed()
while not next(check):
  print('1')

As you asked, the dinner_completed() function returns False only the first time and True all the other times. If you want to set a different threshold you can use the following code:

def dinner_completed():
  n = 0
  threshold = 1
  while n < threshold:
      yield False
      n += 1
  while True:
      yield True

check = dinner_completed()
while not next(check):
    print('1')

Last solution, as stated by in the duplicated answer, you can declare a static variable. I've used the if __name__ == '__main__ to prove that this is not a global variable:

def dinner_completed():
    if dinner_completed.counter==0:
        k=False
    if dinner_completed.counter==1:
        k=True
    dinner_completed.counter+=1
    print('counter',dinner_completed.counter)
    return k
if __name__ == '__main__':
    dinner_completed.counter = 0 
    while not dinner_completed():
        print('1')
trolloldem
  • 669
  • 4
  • 9
  • But U are placing the function in parentheses of next() function. I don't want to edit anything in my while condition. I only want the changes to be made in the function. – Anudeep Kosuri Oct 02 '20 at 08:24
  • @AnudeepKosuri this is the way you call a generator after creating one and placing it in the check variable – trolloldem Oct 02 '20 at 08:27
  • @AnudeepKosuri I've updated the answer to show a third possible solution, like stated in the duplicated answer – trolloldem Oct 02 '20 at 08:38
  • I don't want to change my while condition. Then its better to not use generator concept in this case. I only want to call function as 'while not dinner_completed():' without using next() in your case. The code you written is sending finction as parameter to next but I dont want to do it. – Anudeep Kosuri Oct 02 '20 at 08:39
  • @AnudeepKosuri In the third code sample there is a solution that uses that while condition – trolloldem Oct 02 '20 at 08:40
  • In the code which you have written? – Anudeep Kosuri Oct 02 '20 at 08:42
  • @AnudeepKosuri the third one with the `dinner_completed.counter` static variable and the `if __name__ == '__main__'` statement to prove that it is not a global variable. I've left there the other two solution just to show how to use generators. – trolloldem Oct 02 '20 at 08:44
0

I am unsure about what you want but I guess you can take a look at this?

def dinner_completed(counter):
        return False if counter == 0 else True
       
Ryan Tan
  • 1
  • 1
0

Because of this:

def dinner_completed():
        counter=0
        ...

Each time you enter the function, you initialize the counter to 0. If you don't want to create a global variable, you can send the counter as an argument of the function

def dinner_completed(counter):

But Global variables aren't really the big enemy as you might think and they can be very useful

TeaaParty
  • 11
  • 1