Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return a definition
#1
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 :)
Reply
#2
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.
Reply
#3
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.
Reply
#4
(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
Reply
#5
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.
Reply
#6
Missing the f will do that
Reply
#7
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to coding. An error occurs with no definition westernwhiskey 4 3,109 Mar-15-2018, 12:37 PM
Last Post: westernwhiskey
  trouble importing class definition from one .py into another ijosefson 3 3,422 Oct-16-2017, 08:24 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020