#πŸ”’ Dictionary defenition finder

34 messages Β· Page 1 of 1 (latest)

hallow ledge
#

So im trying to make a script that finds a random word from dictionaryapi and returns the word and its defenition. The problems start at the line labeled "this is the broken line" if used print statements to test and everything before that line works smoothly, ive done print(word) and it prints a random word each time, so the problem is that line im pretty sure. Help please

import requests

def random_word_definition():
try:
word_response = requests.get("https://random-word-api.herokuapp.com/word?number=1")
word_response.raise_for_status()
word = word_response.json()[0]

    dic_response = requests.get(f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}") # this is the broken line

    if dic_response.status_code != 200:
        return None
    dic_data = dic_response.json()
    dic_definition = dic_data[0]["meanings"][0]["definitions"][0]["definition"]

    return "{word}: {dic_definition}"

except Exception as e:
    print("Error:", e)
    return None
blissful magnetBOT
#

@hallow ledge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

hallow ledge
#

idk why it says "%7Bword%7D" btw, it actually says {word}

surreal remnant
#

Are you not getting any response?

final birch
#

It was escaped. I recommend using urljoin to connect the static and variable parts of the url.

surreal remnant
final birch
hallow ledge
#

and its not giving me any error messages too

surreal remnant
#

Try printing the status_code

hallow ledge
#

so i think its getting like stuck on that line somehow

final birch
#

You're exiting immediately if the status code isn't 200. Print the status code and content. Do a raise for status and see what the exception is.

hallow ledge
final birch
#

raise_for_status raises an exception so you need those prints in the except block or before the call.

#

I should say that it raises an exception if the status code >= 400.

hallow ledge
#

sorry im still a bit confused on what you're asking me to do

final birch
#

Before raising, print the status code and content.

hallow ledge
final birch
#

Yes.

hallow ledge
#

prints nothing still

#

its not a problem with what im trying to print by the way, the code just doesnt advance past that line

final birch
#

Can you run it in a debugger? Step through?

#

Is it locking up or exiting entirely?

hallow ledge
#

exiting entirely

final birch
#

Unfortunately I need to go now. Things must be done.

If you have a print immediately after the call to requests.get it should be printing. The whole thing is in a try/except so if there's any exception it should be caught and printing. I am somewhat at a loss at this point.

#

Good luck!

hallow ledge
#

its aight

#

im half convinced its an external issue like the website im using or something

final birch
#

Should still give you some output.

hallow ledge
#

true

blissful magnetBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.