Sep-03-2021, 02:01 AM
(This post was last modified: Sep-03-2021, 02:01 AM by LoveSamosa.)
So the problem I have been given is this:
The user is shown a glossary entry, together with two definitions. One definition is correct and the other incorrect, and they are shown in random order. The user is then invited to indicate which definition they believe is the correct one by typing either 1 or 2. They are then told whether their choice was correct or not.
now I can get two definitions to print (see code below) but for some reason, the order they display doesn't seem to marry up with my code.
So the order in which the definitions should appear is dictated by the variable "correct_def" which is given a random value between 1 and 2 (See below)
then if correct_def is set to anything other than 1 it will print random_key2 first then random_key 1.
The problem is, this doesn't happen 100% of the time, sometimes it will work and then other times it won't.
Any help would be appreciated, I have included the full code below
The user is shown a glossary entry, together with two definitions. One definition is correct and the other incorrect, and they are shown in random order. The user is then invited to indicate which definition they believe is the correct one by typing either 1 or 2. They are then told whether their choice was correct or not.
now I can get two definitions to print (see code below) but for some reason, the order they display doesn't seem to marry up with my code.
So the order in which the definitions should appear is dictated by the variable "correct_def" which is given a random value between 1 and 2 (See below)
# Choose a random order to display the definitions in # '1' means the correct definition # should be printed first. # # '2' means the correct definition # should printed second. # correct_def = choice(['1', '2'])I then tell python to display the definitions as such:
if correct_def == [1]: print (glossary[random_key1]) print (glossary[random_key2]) else: print (glossary[random_key2]) print (glossary[random_key1])so its my understanding that if the correct_def variable is 1 the above code should display the definition for random_key1 first then random_key2
then if correct_def is set to anything other than 1 it will print random_key2 first then random_key 1.
The problem is, this doesn't happen 100% of the time, sometimes it will work and then other times it won't.
Any help would be appreciated, I have included the full code below
def show_flashcard(): """ The user is shown a random glossary entry, along with 2 definitions. The user is asked to say which definition is correct 1 or 2. They are then told whether their answer is correct. """ # Get glossary keys. keys = list(glossary) # Choose two random glossary keys. random_key1 = choice(keys) random_key2 = choice(keys) # Keep checking random_key2 until # it is different from random_key1 while random_key2 == random_key1: random_key2 = choice(keys) # Display random glossary key. print('Here is a glossary entry:',random_key1) # Choose a random order to display the definitions in # '1' means the correct definition # should be printed first. # # '2' means the correct definition # should printed second. # correct_def = choice(['1', '2']) # INSERT YOUR CODE IMMEDIATELY BELOW. print(correct_def) if correct_def == [1]: print (glossary[random_key1]) print (glossary[random_key2]) else: print (glossary[random_key2]) print (glossary[random_key1]) guess=input('which definition is correct? ') if guess == correct_def: print('Correct, well done!') else: print('try again') # DO NOT CHANGE ANYTHING IN THE NEXT SECTION # 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 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.')