lundi 8 novembre 2021

PYTHON | How to avoid repeated value from a list using random string?

I am a complete beginner and would appreciate it if someone could help me avoid repeated value with my random string.

I made up a multiple-choice vocabulary quiz. It selects a random word from my list with four options; This loop repeats 20 times.

Suddenly, Because it's a random choice, it prints repeated values sometimes. I thought to create a list with the last picks and then compare it to the primary index, but it sounds pretty inefficient.

Does someone have any idea to avoid this issue?

This is my code:

    G = 0
    for i in range(20):
   ###   WORDLIST   ###
       list = [question words list.]
       mean = [meaning words list]

       list1 = list.copy()

        mean1 = mean.copy()
        mean2 = mean.copy()
        mean3 = mean.copy()
        mean4 = mean.copy()

        from random import randint
        import random

       word_count = len(list) - 1
       options = []

       options = random.sample(range(1, word_count), 4)

       options1 = options.copy()
       options2 = options.copy()
       options3 = options.copy()
       options4 = options.copy()
       opt1 = options1.pop(0)
       opt2 = options2.pop(1)
       opt3 = options3.pop(2)
       opt4 = options4.pop(3)

       x = randint(0, 3)
       y = options.pop(x)
       # ignore [options]
       question = list.pop(y)
       answer = mean.pop(y)

       print("\n What is the meaning of", question, "?")
       print("1)", mean1.pop(opt1))
       print("2)", mean2.pop(opt2))
       print("3)", mean3.pop(opt3))
       print("4)", mean4.pop(opt4))

       while True:
           try:
            userans = int(input("\nYour answer: "))
            except ValueError:
            print("Sorry, I didn't get that.")
            continue
        else:
            break

    if userans - 1 is x:
        G = (G + 5)
        print("Your answer is correct!\n")
    else:
        if userans == 100:
            print("There are %s words in your personal dictionary" % word_count)
        else:
            if userans > 4:
                print("It's OK if you do not know the answer. Better luck next time:)\nThe meaning of %s is %s!" % (
                    question, answer))
            else:

                print("Your answer is wrong!\nThe meaning of %s is %s!" % (question, answer))
  F = 60
Go = 80
Perfect = 100

if G < F:
    print("Failed. \n Please Try again. \n Your Score is:", G)

elif G > 60 and G < Perfect:
    print("Good Job! \n Your Score is:", G)

if G ==Perfect:
   print("Perfect! \n Your Score is:", G)

Thanks!




Aucun commentaire:

Enregistrer un commentaire