0

So I am very new to Python (and this site) and I am trying to create a flashcard loop for my partner to help her with her studies, I have generated a test version without any real terms and definitions (for now) however when I try to use elif or else in create the loop in Python IDLE 3.11 it just states "Invalid syntax" and showing elif or else with a highlighted 'e', here is the test code for reference:

import random

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

# Set up glossary
glossary = {'word1':'definition1',
            'word2':'definition2',
            'word3':'definition3'}
# Interactive loop
exit = False
while not exit:
    user_input = input('Enter s to show a flashcard and q to quit: ')
    if user_input == 'q':
        exit = True
        elif user_input == 's':
            show_flashcard()
            else:
                print('You need to enter either q or s.')

# Choose a random entry from the glossary
random_word = random.choice(list(glossary.keys()))

# Print the random entry to the screen
print("flashcard:")
print(random_word)

# Wait for the user to press Enter
input("Press Enter to reveal the definition...")

#Print the definition of the entry
print(glossary[random_word])`

I have tested everything before trying to create the loop and it all works, can someone help?

Thank you!

I tried changing the elif and else but no such luck, I don't know any other alternative to this situation.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • The `elif` and `else` need to be on the same indentation level as the `if`. Have you not done a Python tutorial? You should do that first, cause this site isn't built to teach the language basics. Or if you just need a refresher / crash course: [I'm getting an IndentationError. How do I fix it?](/q/45621722/4518341) BTW, welcome to Stack Overflow! Check out [ask] for tips like making a [mre] and how to write a good title. – wjandrea Aug 12 '23 at 22:03
  • also you need to change `glossary()` to just `glossary`, and there's a quote at the end of the file, although that's probably just from posting on SO. I would recommend a syntax highlighting code editor (vim/emacs/vscode), as it would make the formatting issue that @wjandrea mentioned more obvious to see. – toppk Aug 12 '23 at 22:07

1 Answers1

0

it's an indentation issue. You're coding in Python, not some whitespace-insensitive language. Your elif and else statements need to align with the if statement they're associated with.

if user_input == 'q':
    exit = True
elif user_input == 's':
    show_flashcard()
else:
    print('You need to enter either q or s.')
QueryKiller
  • 209
  • 6