Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coding issues
#1
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....

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
Larz60+ write Mar-07-2021, 02:16 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Added code tags for you this time. Also please do not use fancy fonts, color etc. and stick with those provided.
Reply
#2
The results I get with my code added above is:

Enter s to show a flashcard and q to quit: s
Here is a glossary entry:
word2
Here is a possible definition for it:
definition3
Is the definition correct? Enter Y or N: n
Your answer is: Wrong
Enter s to show a flashcard and q to quit: s
Here is a glossary entry:
word3
Here is a possible definition for it:
definition3
Is the definition correct? Enter Y or N: Y
Your answer is: Wrong

As you can see they are both "right" but says that I am wrong, I even tried with both lowercase and uppercase, I am sure that I am missing something simple, I do not want the answer given to me, I just want to be pointed in the right direction so that I can understand why something works the way that it does.

What am I missing basically???????
Reply
#3
indent issue on line 50 (3 instead of 4 spaces)
glossary was never defined nor populated.
Reply
#4
(Mar-08-2021, 02:17 PM)Larz60+ Wrote: indent issue on line 50 (3 instead of 4 spaces)
glossary was never defined nor populated.

Thank you for your reply, I will sort the indentation out, I did not notice that.

Regarding the glossary, it is set out at the bottom below the line where it says "do not change anything beyond this point" as the tutors have set out a majority of the program, all we have to add is the code using a python pattern that we can rearrange to suit the needs for this task.

As I have said, I am using the case analysis - two cases - pattern, as I feel it suits the task in hand, I just feel that I have missed something within the pattern and it is not picking up the code properly thus not giving the correct answer when it asks if is the correct definition, if that makes sense at all?
Reply


Forum Jump:

User Panel Messages

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