Hardware Reference
In-Depth Information
As you write the code, you will include comments . Comments are notes within your
code that explain what a line or section of code in intended to do. Each comment line
begins with the # symbol, which tells the computer running the program to ignore that
line. If a comment wraps over several lines you need to include a # sign at the begin-
ning of each line so that it is passed over by the IDE.
here are many good reasons for including comments inside your code. Comments can
help you remember what each part of the code is doing, should you leave it uninished
for a while. In school, you may use comments to explain to your teacher what each part
of your code is doing. If you are working with others, comments help them see what
you have done already.
1. Begin your code with a comment line to indicate the code's purpose. Open the
Inventory.py ile that you created earlier and type the following line at the
top (above the inventory list that is already in the ile):
# Adventures in Raspberry Pi Python - Inventory
Note the # symbol at the start of the line, identifying it as a comment.
2. Next, use the import command to import the two Python modules you need,
time and random . You can add a comment to explain this step if you like, as
shown in the following code:
# You will need the random module and the time module.
import random
import time
3. Press Enter to leave a blank line so that your code will be easier to read and then
use the print() function to display two strings of text on the screen:
# Enter a blank line here
print(“You have reached the opening of a cave”)
print (“you decide to arm yourself with a ”)
4. Next, use the sleep() function from the time module to make the program wait
for two seconds before asking the player a question by adding the argument (2)
as shown.
time.sleep(2)
An argument is a piece of information given to the function that it may use to
perform its task. The argument goes inside the brackets that follow the function
name. In the code in step 5, for example, with the time.sleep() function you
use the argument (2) , which is the number of seconds you want the program to
wait before implementing the next line.
Search WWH ::




Custom Search