1
LETTERS = "abc"
correct = "cab "
guess = ""

while guess != correct:
    for i in LETTERS:
        position = random.randrange(len(LETTERS))
        guess += LETTERS[position]
        LETTERS = LETTERS[:position] + LETTERS[(position + 1):]
    print(guess)

I'm new in Python and I want to make this simple program:

  • With the letters "abc", jumble them and create a new three-lettter word randomly.
  • Print that jumble
  • Continue doing this loop until the computer jumbles "cab".
  • Then stop.

I came up with this code, and it gives me an infinite loop. I can't figure out why is doing it. I'm sure it's something easy but I can't see it. Need some help! Thanks!

3
  • 2
    Did you mean to end your correct value with a space? Commented Jan 23, 2014 at 19:14
  • 1
    And did you mean to reset your guesses after they fail? Commented Jan 23, 2014 at 19:15
  • 2
    OP is also altering LETTERS on every iteration of the for-loop. Needs to alter a copy of LETTERS instead with a myLetters = LETTERS[:] immediately inside the while-loop (and reset guess too) Commented Jan 23, 2014 at 19:15

2 Answers 2

3

You have three problems that I can see:

  1. "cab " has a space in it, and LETTERS does not have a space. So you'll never be able to guess a space
  2. You don't reset guess. You simply keep adding to it
  3. You change LETTERS in your for-loop, so in the second iteration of your while-loop, it will be completely empty.

This is how I would go about doing what you're trying to do (with minimal modification):

_LETTERS = "abc"
correct = "cab"
guess = ""

while guess != correct:
    LETTERS = _LETTERS[:]
    guess = ""
    for i in LETTERS:
        position = random.randrange(len(LETTERS))
        guess += LETTERS[position]
        LETTERS = LETTERS[:position] + LETTERS[(position + 1):]
    print(guess)

Here's how I would do a random search (which is what you're trying to do):

guess = "abc"
correct = "cab"

while guess != correct:
    guess = list(guess)
    random.shuffle(guess)
    guess = ''.join(guess)
    print(guess)
print(guess)

Of course, there are better techniques to correctly guess "cab". If you really want to try an exhaustive search, then you could use a backtracking DFS:

def DFS(letters, correct, sofar=None)
    if sofar is None:
        sofar = ''
    if not letters:
        if sofar == correct:
            print("Yay! I found it")
        else:
            print("Oops! I found %s instead" %sofar)
    else:
        for i,char in enumerate(letters):
            DFS(letters[:i]+letters[i+1:], correct, sofar+char)
Sign up to request clarification or add additional context in comments.

5 Comments

Your second code snippet doesn't work. You're throwing away the shuffled list.
@user2357112: False. random.shuffle is IN-PLACE. It returns None
In-place on a list you immediately throw away.
@user2357112: good catch. I totally missed that. It's fixed now. Thanks
Thank you for all your answers! That space at "cab" was a typo. It wasn't meant to be there! I see now what was the problem haha, what a silly thing. Now it works fine. Also thank you for the random.shuffle thing. I didn't know that one!
2

Your correct value contains a space, but your loop never generates spaces:

correct = "cab "

Remove that space:

correct = "cab"

Next, your loop reduces LETTERS to an empty string, so only once does your loop produce a random guess, but afterwards, you forever are stuck with LETTERS = '', so no for loop is run.

You'd be better off using random.shuffle to produce guesses:

LETTERS = list("abc")
correct = "cab"

while True:
    random.shuffle(LETTERS)
    guess = ''.join(LETTERS)
    if guess == correct:
        print(guess)
        break

Comments

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.