Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why wont this work?
#1
from random import *

def show_flashcard():
    """ Show the user a random key and ask them
        to define it. Show the definition
        when the user presses return.    
    """
    random_key = choice(list(glossary))
    print('Define: ', random_key)
    input('Press return to see the definition')
    print(glossary[random_key])


def look_up_definition():
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 + ' is ' + glossary[user_input]
        

    
    

# Set up the glossary

glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}

# The interactive loop

exit = False
while not exit:
    user_input = input('Enter s to show a flashcard, d to look up the definition and q to quit: ')
    if user_input == 'q':
        exit = True
    elif user_input == 'd':
        look_up_definition()
    elif user_input == 's':
         show_flashcard()
    else:
        print('You need to enter either q, d or s.')
So i asked for help the other day and got it working, the next bit needs you to amend the interactive loop so the user can press d to look up the definition. I just don't understand why it wont work? I can run the def look_up_definition in the shell but it wont work in the loop. It is probs something small and i just completely overlooked it.

Any hints would be awesome :)
Reply
#2
For starters
def look_up_definition():
user_input = input('Please enter your word: ')
user_input line needs indenting into the function
Reply
#3
What exactly doesn't work? Note that your look_up_definition function returns a value, but on line 39, you do nothing with that value - it's just thrown away. If this is what's working in the REPL, but not here, that's expected - the job of the REPL is to print the values of expressions (like the return value of a function call). In scripts, etc. you aren't always going to print things, sp it wouldn't make sense for the interpreter to do that.
Reply
#4
(Jul-30-2020, 05:00 PM)ndc85430 Wrote: What exactly doesn't work? Note that your look_up_definition function returns a value, but on line 39, you do nothing with that value - it's just thrown away. If this is what's working in the REPL, but not here, that's expected - the job of the REPL is to print the values of expressions (like the return value of a function call). In scripts, etc. you aren't always going to print things, sp it wouldn't make sense for the interpreter to do that.

So when i run the code, i press s and it runs the code, i press d and it only lets me type the word and wont run the rest of the def. I guessing it has something to do with the loop?
Reply
#5
It runs the rest of the function, but the function does this:
def look_up_definition():
    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 + ' is ' + glossary[user_input]
Unlike show_flashcard() that prints something, look_up_definition() only returns a string. No printing. Either replace the "return" with "print" or print the function result in the while loop.

user_input is a string, so str(user_input) doesn't do anything.

Do you know about the "break" command? You could get rid of that ugly while loop.
Reply
#6
(Jul-30-2020, 07:44 PM)deanhystad Wrote: It runs the rest of the function, but the function does this:
def look_up_definition():
    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 + ' is ' + glossary[user_input]
Unlike show_flashcard() that prints something, look_up_definition() only returns a string. No printing. Either replace the "return" with "print" or print the function result in the while loop.

user_input is a string, so str(user_input) doesn't do anything.

Do you know about the "break" command? You could get rid of that ugly while loop.

Simple reason again XD they haven't taught us that yet, so i best not use in the work, they'll mark you down for not using it. But thank you!!!
Reply
#7
Just some comments:

from random import *
Python documentation:

Quote:This imports all names except those beginning with an underscore (_). In most cases Python programmers do not use this facility since it introduces an unknown set of names into the interpreter, possibly hiding some things you have already defined.

Note that in general the practice of importing * from a module or package is frowned upon, since it often causes poorly readable code. However, it is okay to use it to save typing in interactive sessions.

if user_input not in list(glossary):
glossary is dictionary and there is no need to convert into list:

if user_input not in glossary:
f-strings are available for many years now so instead of:

'The definition of ' + user_input + ' is ' + glossary[user_input]
one can write:

f'The definition of {user_input} is {glossary[user_input]}'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  my simple code wont work! help! simon12323121 2 2,033 Sep-05-2021, 09:06 AM
Last Post: naughtyCat
  MyProgrammingLab wont accept anything I put in chicks4 2 11,578 Feb-10-2019, 11:44 PM
Last Post: chicks4

Forum Jump:

User Panel Messages

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