简体   繁体   中英

How do I get this while loop and json response to work?

The first time the script is run with input userid and if the user is online, it runs fine until the print(f'{username}, {userid}' + ' is still online...') gets stuck. The program never continues to the else: when the user go offline and while userid == str(ps['user_id']): is no longer True .

It's like the str(ps['user_id']): never updates in the script when called.

userid = input('Input userID: ')
response = requests.post('website.com/api', headers=headers, data=data)
json_data = json.dumps(response.json(), indent=2)
data = json.loads(json_data)

while True: # Main loop to run if a user is online
    for ps in data['result']['page']['list']:
        if userid == str(ps['user_id']): # If a user is online print details
            username = ps['nick_name']
            print('Username: ' + ps['nick_name'])
            print('UserID: ' + str(ps['user_id']))           
            while userid == str(ps['user_id']): # Look if the user is still online (is in the json response)
                print(f'{username}, {userid}' + ' is still online...')
                time.sleep(3)
        else: # If user go offline(is not in the json response), break and restart main loop(first while loop).
            break

        print('Waiting for ' + f'{userid}' + ' to get online...') # Message until user go online again (is found in the json response).
        time.sleep(5)

You are not updating data anywhere inside of your loop so its using the same stale data for every iteration. You can just add your three lines to generate data into your loop before the time.sleep(5). This will give you updated data and should resolve your issue.

userid = input('Input userID: ')
response = requests.post('website.com/api', headers=headers, data=data)
json_data = json.dumps(response.json(), indent=2)
data = json.loads(json_data)
userList = data['result']['page']['list']
isOnline = 0

while True: # Main loop to run if a user is online
    hasLoggedIn = 0
    for user in userList:

        if str(user['user_id']) == userid and isOnline == 0: # If a user is online print details
            username = user['nick_name']
            print('Username: ' + user['nick_name'])
            print('UserID: ' + str(user['user_id']))
            print(f'{username}, {userid}' + ' is now online...')
            isOnline = 1
            hasLoggedIn = 1
            time.sleep(3)

        elif str(user['user_id']) == userid and isOnline == 1:       
            print(f'{username}, {userid}' + ' is still online...')
            hasLoggedIn = 1
            time.sleep(3)

    if hasLoggedIn == 0:    
        print('Waiting for ' + f'{userid}' + ' to get online...') # Message until user go online again (is found in the json response).
        isOnline = 0
        time.sleep(5)

    response = requests.post('website.com/api', headers=headers, data=data)
    json_data = json.dumps(response.json(), indent=2)
    data = json.loads(json_data)
    userList = data['result']['page']['list']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM