Python Forum
Return a definition - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Return a definition (/thread-28627.html)



Return a definition - samh625 - Jul-27-2020

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 :)


RE: Return a definition - jefsummers - Jul-27-2020

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.


RE: Return a definition - deanhystad - Jul-27-2020

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.


RE: Return a definition - samh625 - Jul-27-2020

(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


RE: Return a definition - deanhystad - Jul-27-2020

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.


RE: Return a definition - jefsummers - Jul-27-2020

Missing the f will do that


RE: Return a definition - samh625 - Jul-28-2020

(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!