Python Forum
Help with a python problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with a python problem
#1
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)

# 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.')
Reply
#2
use: if correct_def == '1':
not if correct_def == [1]:
Reply
#3
Hi Larz,

I've changed the code as you suggested and this has fixed it.

Thank you so much you are a life saver!
Reply
#4
As an aside, you want to pick 2 unique keys. So you pick randomly until you get two different answers. But you can directly pick two distinct objects. Take a look at random.samples().
Reply
#5
(Sep-03-2021, 11:55 PM)bowlofred Wrote: As an aside, you want to pick 2 unique keys. So you pick randomly until you get two different answers. But you can directly pick two distinct objects. Take a look at random.samples().

For that the assessor only wanted us to organise the order of which the definitions were displayed it and how the user interacted. The random selection was pre-set for us.

But I've added your suggestion to my scrap book of tips so I can use that moving forward.

Would I be right in thinking that random.samples() would ensure that no two keys picked would be the same and delete the need for the loop of selecting until the keys are different?
Reply
#6
Ur comparing a list containing 1
So the computer
is like:
Quote:Hmmm lets see
What value is correct_def
1 Ok
Then it reaches the if statement
And it does:
Quote:lets see if correct def is a 1
Inside a list:
No, its just and int
(Not a list)
So you would have to do:
if correct_def == 1:
       print (glossary[random_key1])
       print (glossary[random_key2])
   else:
       print (glossary[random_key2])
       print (glossary[random_key1])
Reply


Forum Jump:

User Panel Messages

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