Reputation: 7
words = ['a', 'b', 'c', 'd']
code = ['3' , '2', '4' , '9']
I have two lists that the user must try and match together. In order to do this, they have to alter the 'code' list:
number = input("Enter a number: ")
letter = input("Enter the letter: ")
code =list(map(lambda x: str.replace(x, number, letter), code))
How can I loop this so that the user has to keep changing the numbers for the 'code' list until it matches the 'words' list?
I've tried using
while code != words:
but it doesn't work properly because during the second time, the changes I made from the first instance aren't present.
Upvotes: 0
Views: 250
Reputation: 7146
I'm guessing that what you actually want is two loops, one for waiting until they guess right, and one for gathering their guess:
words = ['a', 'b', 'c', 'd']
code = ['3' , '2', '4' , '9']
guess = None
while guess != words:
print 'Enter 4 number/letter pairs'
guess = code
for i in range(4):
number = raw_input("Enter a number: ")
letter = raw_input("Enter the letter: ")
guess = list(map(lambda x: str.replace(x, number, letter), guess))
if guess == words:
print 'You got it!'
else:
print 'Nope! Guess again!'
This way the guess is reset each time. If you don't do that, then after 4 times the working list for the code (guess
in my code here) might be something like this:
>>> print guess
['3', 'd', 'y', '9']
If it looks like this, then to fix it, they would have to enter a letter/letter pair, rather than a number/letter pair. To make things worse, you could even have it get into a state where it becomes impossible to get it right. For example, if guess
looks like this:
['3', 'a', 'a', '9']
If you try to change one of the 'a'
s to anything else, then the other 'a'
will change as well. Seeing as the result you're trying to get has them as being different, then there is no way to get the correct result from this guess
.
If you really do want each guess to carry on until the user gets it all correct, I'd suggest a different strategy:
words = ['a', 'b', 'c', 'd']
code = ['3' , '2', '4' , '9']
mapping = {}
guess = None
while guess != words:
number = raw_input("Enter a number: ")
letter = raw_input("Enter the letter: ")
mapping[number] = letter
guess = [mapping.get(entry, entry) for entry in code]
print 'You got it!'
This strategy will overwrite any previous guess and effectively make it so they are always working with the original code.
Whatever strategy you choose, it would probably be a good idea to give the user some idea of what they've already guessed. In my second example, you could print out the mapping (in some nice format). Here's an idea for how you could print it:
print '\n'.join('%s->%s' % (number, letter) for number, letter in sorted(test.items(), key=lambda x: x[0]))
This would print {'1': 'a', '3': 'c', '2': 'b'}
like this:
1->a
2->b
3->c
Upvotes: 0
Reputation: 9115
Is the code list invariant?
If so, you could do the following:
def guess(code, words):
print('The Code Is: '+str(code))
guess = raw_input('Enter A List Of Letters, Separated by Commas: ')
guess = guess.split(',')
guess = [letter.strip() for letter in guess]
number_correct = 0
for index in range(min(len(guess), len(words))):
if guess[index] = words[index]
number_correct += 1
if number_correct == len(words):
print('You Got It!')
return True
else:
print('You Got {0} of {1} Letters Right! Try Again...'.format(number_correct, len(words))
return guess(code, words)
Upvotes: 2
Reputation: 18487
I think you are just not reassigning the user's guess (the lambda manipulation) to the proper code
variable. I'm guessing through the question, but this seems like what you are trying to do:
words = ['a', 'b', 'c', 'd']
code = ['3' , '2', '4', '9']
while words != code:
number = raw_input("Enter a number: ")
letter = raw_input("Enter the letter: ")
code = list(map(lambda x: str.replace(x, number, letter), code))
print('You finally got it!')
Upvotes: 0