mercredi 18 novembre 2020

Word guessing game - problem with picking random word every time the loop runs

I want to get a new word choice every time I answer one word but I'm facing issue with getting a new random word from the word list even after using random.shuffle().

My answer:

  1. Create a dictionary (keys, values) of german words by extracting from csv file where keys will be in german, values in english

  2. You want to display a random value from the dict and ask the user to enter the meaning

    import csv
    import random
    play_game = input(' Press \'y\' for yes and \'n\' for no')
    while play_game == 'y':
        with open('/Users/pydev97/Desktop/wordlist.csv', mode='r') as csv_file:
            csv_reader = csv.DictReader(csv_file)
            for row in csv_reader:
                keys = row['MEANING']
                values = row['WORDS']
                new_dict = dict({keys : values})
    
        keys_list = list(new_dict.keys()) # converting dict values to a list
        random.shuffle(keys_list)
        for word in keys_list:
          display = '{}'
          print(display.format(word))
          user_answer = input('Answer : ')
    
          if user_answer == new_dict[word]:
             print('You got it right')
          else:
             print('You got it wrong')
    

Output I'm getting:

Press 'y' for yes and 'n' for noy
to participate
Answer : dd
You got it wrong
to participate
Answer : es
You got it wrong
to participate
Answer : mit machen/teil nehmen
You got it right
to participate
Answer :



Aucun commentaire:

Enregistrer un commentaire