Game Development Reference
In-Depth Information
into these lines:
9. number = random.randint(1, 100 )
10. print('Well, ' + name + ', I am thinking of a number
between 1 and 100 .')
And now the computer will think of an integer between 1 and 100 . Changing line 9 will
change the range of the random number, but remember to change line 10 so that the game
also tells the player the new range instead of the old one.
Calling Functions that are Inside Modules
By the way, be sure to enter random.randint(1, 20) and not just randint(1,
20) , or the computer will not know to look in the random module for the randint()
function and you'll get an error like this:
>>> randint(1, 20)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'randint' is not defined
>>>
Remember, your program needs to run import random before it can call the
random.randint() function. This is why import statements usually go at the
beginning of the program.
Passing Arguments to Functions
The integer values between the parentheses in the random.randint(1, 20)
function call are called arguments. Arguments are the values that are passed to a function
when the function is called. Arguments tell the function how to behave. Just like the
player's input changes how our program behaves, arguments are inputs for functions.
Some functions require that you pass them values when you call them. For example, look
at these function calls:
input()
print('Hello')
random.randint(1, 20)
The input() function has no arguments but the print() function call has one and
the randint() function call has two. When we have more than one argument, we separate
Search WWH ::




Custom Search