Game Development Reference
In-Depth Information
11 - Bagels
to the user.
13. if guess == secretNum:
14. return 'You got it!'
The getClues() function will return a string with the fermi, pico, and bagels clues,
depending on what it is passed for the guess and secretNum parameters. The most
obvious and easiest step is to check if the guess is the exact same as the secret number. In
that case, we can just return 'You got it!' .
16. clue = []
17.
18. for i in range(len(guess)):
19. if guess[i] == secretNum[i]:
20. clue.append('Fermi')
21. elif guess[i] in secretNum:
22. clue.append('Pico')
If the guess is not the exact same as the secret number, we need to figure out what clues to
give the player. First we'll set up a list named clue , which we will add the strings
'Fermi' and 'Pico' as needed. We will combine the strings in this list into a single
string to return.
We do this by looping through each possible index in guess and secretNum (they both
are the same size). We will assume that guess and secretNum are the same size. As the
value of i changes from 0 to 1 to 2 , and so on, the if statement checks if the first, second,
third, etc. letter of guess is the same as the number in the same position in secretNum . If
so, we will add a string 'Fermi' to clue.
If that condition is False we will check if the number at the i th position in guess
exists anywhere in secretNum . If this condition is True we know that the number is
somewhere in the secret number but not in the same position. This is why we add the
'Pico' to clue .
23. if len(clue) == 0:
24. return 'Bagels'
If we go through the entire for loop above and never add anything to the clue list, then
we know that there are no correct digits at all in guess . In this case, we should just return
the string 'Bagels' as our only clue.
Search WWH ::




Custom Search