Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple counter
#11
Why doesn't time.sleep(speed/2) on line 14 have any affect?

import time
def earnings():

    speed = 1
    i = 0
    while True:
        i += 1
        time.sleep(speed)
        print(i, end = '\r')

        if earnings() == 10:
            buy = input('Do you want to buy more? ')
            if buy == 'y':
                time.sleep(speed/2)
                print(i, end = '\r')

earnings()
Reply
#12
Have you notice that it dos not start to count.
The loop check this line in every loop.
if earnings() == 10:
Will call function in a recursive loop with 1 as outcome.

i is the Incremental variable so:
if i == 10:
Here a fix but the second sleep will only run 1 time,added time to make it clear.
Then it will loop Incremental to infinity Sleepy
import time

def earnings():
    speed = 1
    i = 0
    while True:
        # As the loop has not been break out of **
        # 11,12,13,14...now loop forever as will not be equal to 10 anymore
        i += 1
        time.sleep(speed)
        print(i, end='\r')
        if i == 10:
            buy = input('Do you want to buy more? ')
            if buy == 'y':
                time.sleep(speed + 10) # will run 1 time **
                print(i, end='\r')

earnings()
Reply
#13
How do I break out of the loop when i == 10 and then restart the loop with i = 10 and a new sleep time?
Reply
#14
(Feb-04-2018, 04:54 AM)mcmxl22 Wrote: How do I break out of the loop when i == 10 and then restart the loop with i = 10 and a new sleep time?
You have to use break and then start a new loop.
It easy to go down a rabbit hole if try to do to much of this(while loop) in one function.

Here a setup that also fall back to a menu,which can restart earnings function or Quit out.
So here just use speed and i that already set and set stop at 20,
then the new while loop will now run in a different interval 3-sec.
When Finnish it fall back to menu.
import time

def earnings():
    speed = 1
    i = 0
    while True:
        i += 1
        time.sleep(speed)
        print(i, end='\r')
        if i == 10:
            break
    buy = input('Do you want to buy more? ')
    while True:
        if buy == 'y':
            i += 1
            time.sleep(speed + 3)
            print(i, end='\r')
            if i == 20:
                return input('Press enter to return to menu')

def menu():
    while True:
        print('(1) Earnings')
        print('(Q) Quit\n')
        choice = input('Enter your choice: ').lower()
        if choice == '1':
            earnings()
        elif choice == 'q':
            return False
        else:
            print(f'Not a correct choice: {choice}')

if __name__ == '__main__':
    menu()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple CPS counter GalaxyCoyote 1 5,860 Jun-09-2019, 07:59 PM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020