0

I am creating an investing bot. Every hour it runs a function that makes predictions every hour. In order to test the bot, I give it a variable called balance = 1000. I want to only pass it the first time I run it, because the following times the function is being called, the balance variable is changed (in order to see how much money the bot is making throughout the test, because every run is an hour).

The scheduling function I am using is the following:

schedule.every().hour.at(":01").do(main)

while True:
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    schedule.run_pending()
    print(" I'm running : " + current_time)
    time.sleep(60) 

If I declare the variable balance inside the function main, the balance will return to 1000 every time the function is run. How should I proceed?

khelwood
  • 55,782
  • 14
  • 81
  • 108
robocop96
  • 1
  • 1

1 Answers1

1

As you say it won't work by declaring balance in the main() function. Try to make it global. It's not the most elegant but the most immediate to try.


balance = 0

schedule.every().hour.at(":01").do(main)

while True:
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    schedule.run_pending()
    print(" I'm running : " + current_time)
    time.sleep(60)

def main():
    global balance
    balance = do_some_computation()
0x0fba
  • 1,520
  • 1
  • 1
  • 11