0

In my program 6 letters will be randomly generated every time and given to the user. I want the user to then only be able to input using those 6 letters,and if they don't it will be an error. When I try to use a for loop it repeats the code the number of letters the user entered times. When I use regex it only accepts it if it is exactly the same. How could I fix this?

Code

from random_word import RandomWords
r = RandomWords()

print("WELCOME TO THE ANAGRAM GAME!")
word = r.get_random_word(minLength = 6, maxLength = 6)
print(word)

done = time.time() + 60 * 1
while time.time() < done:
    q1 = input("Enter your anagrams")
    if re.findall(word, q1):
        print("Correct")
        answers = []
        answers.append(q1)
        print(answers)
        score = 0
    else:
        print("Wrong")
2
  • Can you share your code? Commented May 23, 2020 at 3:15
  • I have added my code, Commented May 23, 2020 at 3:21

2 Answers 2

2

Compare the sets of letters:

if set(word) >= set(q1): # Same (or fewer) letters

Operator >= checks if the right operand is a subset of the left operand.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have those random letters in a list:

if set(userInput).issubset(set(randomLetters)):
    #dosomething

1 Comment

Unfortunately it only accepts it as right if it is the exact same. I am trying to code it that,the user can have their input shorter than 6 letters as long as they only use the letters given to them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.