Game Development Reference
In-Depth Information
the same happens with numbers[1] (that is, 8 ) and on the third iteration the same
happens with numbers[2] (that is, 3 ). The final value of secretNum that is returned is
'983' .
You may notice that secretNum in this function is a string, not an integer. This may
seem odd, but remember that our secret number could be something like '012' . If we
stored this as an integer, it would be 12 (without the leading zero) which would make it
harder to work with in our program.
Augmented Assignment Operators
The += operator on line 8 is new. This is one of the augmented assignment operators.
Normally, if you wanted to add or concatenate a value to a variable, you would use code
that looked like this:
spam = 42
spam = spam + 10
cheese = 'Hello '
cheese = cheese + 'world!'
After running the above code, spam would have the value 52 and cheese would have
the value 'Hello world!' . The augmented assignment operators are a shortcut that
frees you from retyping the variable name. The following code does the exact same thing as
the above code:
spam = 42
spam += 10 # Same as spam = spam + 10
cheese = 'Hello '
cheese += 'world!' # Same as cheese = cheese +
'world!'
There are other augmented assignment operators. -= will subtract a value from an
integer. *= will multiply the variable by a value. /= will divide a variable by a value.
Notice that these augmented assignment operators do the same math operations as the - , * ,
and / operators. Augmented assignment operators are a neat shortcut.
How the Code Works: Lines 11 to 24
We also need a way of figuring out which clues to show to the player.
11. def getClues(guess, secretNum):
12. # Returns a string with the pico, fermi, bagels clues
Search WWH ::




Custom Search