Python Forum

Full Version: Return a definition
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys! so i have been able to get to almost all of this question correct, which i am quite proud of!!!

But this last bit is not showing the correct information.

def look_up_definition():
    """ To find the user the definition of the user's input and priint
    a suitable message if found or not found
    """
    user_input = input('Please enter your word: ')
    if user_input not in list(glossary):
        return(str(user_input) + ' not found in glossary.')
    else:
        return ('The definition of') ,user_input + (glossary [user_input])
I need the
return ('The definition of') ,user_input + (glossary [user_input])
To say The definition of "example" word1 is definition1. But at the moment it keeps returning as ('The definition of', 'word1definition1').

I cant for the life of me figure out why, when i try another way, it keeps saying syntax error on the word definition.

Any insight would be awesome :)
This is only part of your code, and not sure where it is getting glossary, but I believe the following should work
return f'The definition of {user_input} is {glossary [user_input]}'
If this does not work please post all of your code, as the error may be elsewhere.
user_input is a string, so str(user_input) does nothing. I don't understand what you are doing with the parenthesis in this line:
# return ('The definition of') ,user_input + (glossary [user_input])
return 'The definition of ' + user_input + ' is ' + glossary [user_input]
I think a format string is a better solution (f'' or ''.format()), but you can use string concatenation if you want.
(Jul-27-2020, 11:23 AM)jefsummers Wrote: [ -> ]This is only part of your code, and not sure where it is getting glossary, but I believe the following should work
return f'The definition of {user_input} is {glossary [user_input]}'
If this does not work please post all of your code, as the error may be elsewhere.

Thank you, it didn't work my end, the glossary is part of the rest of the code, i should of posted it.

This is what it came back with 'The definition of {user_input} is {glossary [user_input]}' when i ran it
Are you missing the f prefix that marks this as a format string? What you report is what would happen if you leave out the f prefix.
Missing the f will do that
(Jul-27-2020, 01:04 PM)deanhystad Wrote: [ -> ]user_input is a string, so str(user_input) does nothing. I don't understand what you are doing with the parenthesis in this line:
# return ('The definition of') ,user_input + (glossary [user_input])
return 'The definition of ' + user_input + ' is ' + glossary [user_input]
I think a format string is a better solution (f'' or ''.format()), but you can use string concatenation if you want.
Thank you! that one worked a treat!