title: While loops polling a json response api properly
body: <p>I have this script where I call an api with json response and I want the script to see if a user is online. If the user is online then TTS that the user is online. If the user is offline then keep polling for when the user gets online. If the user is online but go offline, then go back to polling for the next time the user gets online.</p>
  
  <p>The issue is, that there's apparently something wrong with my loops to achieve this.
  As it is now, when a user is online, it just repeats the <code>print(f'{username}, {userid}' + ' is still online...')</code> even if the user go offline.</p>
  
  <p>If the user is offline then the <code>print('Waiting for ' + f'{userid}' + ' to get online...')</code> just repeats and the script never starts if the user gets online.</p>
  
  <p>What am I doing wrong totally wrong here?</p>
  
  <pre><code>while True: # Main loop to run if a user is online
      json_data = json.dumps(response.json(), indent=2)
      data = json.loads(json_data)
      duration = 0.6  # seconds
      freq = 440  # Hz
      for ps in data['result']['page']['list']:
          if userid == str(ps['user_id']): # If a user is online, notify and print details
              username = ps['nick_name']
              Text = "User" f'{username}' "is online"
              TTS = gTTS(text=Text, lang='en-uk')
              TTS.save("voice.mp3")            
              os.system('play -nq -t alsa synth {} sine {}'.format(duration, freq))
              os.system("mpg123 voice.mp3")
              print('Username: ' + ps['nick_name'])
              print('UserID: ' + str(ps['user_id']))           
          while userid == str(ps['user_id']): # Look for when the user go offline
              print(f'{username}, {userid}' + ' is still online...')
              time.sleep(3)
          else: # If user go offline, break and continue polling for when the user gets online
              break
  
      print('Waiting for ' + f'{userid}' + ' to get online...')
      time.sleep(5)
  </code></pre>