Ошибка с break при написании цикла в функции

Рейтинг: 0Ответов: 1Опубликовано: 13.02.2023

Делаю функцию, которая спрашивает у пользователя, есть ли у него oAuth-токен для Twitch.

import requests


def check_oAuth():
    YesOrNo = input("Do you have oAuth code?(Y/N): ")
    for j in YesOrNo:
        if j == "Yes" or "Y":
            break
    if YesOrNo == "No" or "N":
    client_id = input("Paste your client_id: ") 
    client_secret = input("Paste your client_secret: ") 

# Request an OAuth token
    url = 'https://id.twitch.tv/oauth2/token'
    data = {
        'client_id': client_id,
        'client_secret': client_secret,
        'grant_type': 'client_credentials'
    }
    response = requests.post(url, data=data)


# Extract the OAuth token from the response
    if response.status_code == 200:
        data = response.json()
        token = data['access_token']
        print("Your oAuth token is: " + token)

В итоге, если при вопросе ответить Yes или Y, то код не прерывается.

Ответы

▲ 2Принят

input() всегда возвращает строку. Ты вводишь строку, поэтому должен сравнить всю строку. Используя for ты будешь перебирать строку по буквам, что неправильно в данном случае, надо сравнить вводимую строку.

def check_oAuth():
    YesOrNo = input("Do you have oAuth code?(Y/N): ")
    if (YesOrNo == "Yes") or (YesOrNo == "Y"):
        return 0
    if (YesOrNo == "No") or (YesOrNo == "N"):
        client_id = input("Paste your client_id: ")
        client_secret = input("Paste your client_secret: ")