Mar-06-2021, 11:11 PM
Hi all,
I am fairly new to coding and python, I am studying it, and would like some help on a question that I have.
The question is:
They [the user] are then shown a glossary entry, together with a definition that is not necessarily correct, and asked to say if the definition is correct or not. They are then told whether their response was right or wrong.
We have been given a starting and ending part of the code and I have to add the middle, however, I am finding that it is not working properly....
I have highlighted my part in purple.
No matter what the random pairing is, I keep getting 'Wrong' as the answer at the moment, even if it is right. I am using a case analysis - two cases pattern.
Can someone please let me know where I am going wrong, I have been working on this for a couple of weeks now, and it goes from constantly being the 'Right' answer one day, then the 'Wrong' answer the next, and now I only have a few days to get my assignment in.
Many thanks,
Rooroo
I am fairly new to coding and python, I am studying it, and would like some help on a question that I have.
The question is:
They [the user] are then shown a glossary entry, together with a definition that is not necessarily correct, and asked to say if the definition is correct or not. They are then told whether their response was right or wrong.
We have been given a starting and ending part of the code and I have to add the middle, however, I am finding that it is not working properly....
from random import * def show_flashcard(): """ The user is shown a random glossary entry, together with a definition that is not necessarily correct, and asked to say whether the definition is correct (Y) or not (N). They are then told whether their response was right or wrong. """ """ Do not change this first section.""" # Get glossary keys. keys = list(glossary) # Choose two distinct keys at random. sample_keys = sample(keys, 2) # The first is the entry that will be displayed. # The definition corresponding to this is the right one. random_entry = sample_keys[0] # The second entry will not be displayed. # The definition corresponding to this is the wrong one. other_random_entry = sample_keys[1] # This variable will control whether the user is # shown the right definition or the wrong one. right_or_wrong = choice(['right', 'wrong']) # Pose question. print('Here is a glossary entry:') print(random_entry) print('Here is a a possible definition for it:') if right_or_wrong == 'right': print(glossary[random_entry]) else: print(glossary[other_random_entry]) """ End of first section.""" """ Insert your code below.""" # --------------------------- My Code -------------------------- """To define the glossary and the definition, so that we can and match them correctly. The user is then to decide whether the random given glossary choice and the random given definition choice match by either entering a Y or an N. They are told whether their choice of Y or N of the match is either right or wrong.""" #Initialise the input vaiables for 'Y' or 'N' if glossary[random_entry] == glossary[other_random_entry]: true = 'Y' else: if glossary[random_entry] != glossary[other_random_entry]: false = 'N' #chooses whether the answer the user inputs is right or wrong if glossary[random_entry] == glossary[other_random_entry]: answer = 'Right' else: if glossary[random_entry] != glossary[other_random_entry]: answer = 'Wrong' #Achieve the user input of Y or N user_input = input('Is the definition correct? Enter Y or N: ') #Print whether the user response is Right or Wrong print('Your answer is: ', answer) # --------------------------- End My Code -------------------------- """ Do not change anything beyond this point """ # 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.')
I have highlighted my part in purple.
No matter what the random pairing is, I keep getting 'Wrong' as the answer at the moment, even if it is right. I am using a case analysis - two cases pattern.
Can someone please let me know where I am going wrong, I have been working on this for a couple of weeks now, and it goes from constantly being the 'Right' answer one day, then the 'Wrong' answer the next, and now I only have a few days to get my assignment in.
Many thanks,
Rooroo