Uploaded by frogandfeels

labProgramming

advertisement
CISB 11 Programming Lab
In a web browser, go to www.onlinegdb.com.
At the top right of the window, in the Language menu, choose Python 3.
The window should look very similar to this:
Make sure the filename is now main.py. This is the name of our source code file.
Source code is all the instructions written in a programming language for our program.
In a column to the left of the source code, we see line numbers.
The line numbers are not part of the program. They are there to help us refer to specific
parts of the source code.
Lines 1 through 8 are a comment. The computer ignores comments when it runs the
program.
Comments are very important for humans to understand complicated code.
Currently, the only instruction statement in our program is a print statement on line 9.
CISB 11 Programming Lab
In line 9, the word print is the name of a function. A function can receive a list of input
values. Then it will do some work using those values and may return a value back to our
program.
We write the list of input values inside a pair of parentheses after the function name. The
formal name for a function’s input values is arguments.
A string is a sequence of characters like the letters in the alphabet, numerical digits, or
punctuation. The empty space character is a character too.
If we type in a string explicitly in our code we have to enclose the string inside a pair of
quotation marks. In the Python language, we can use a pair of single quotes or a pair of
double quotes.
On line 9, the print function has only 1 argument, the string 'Hello World'.
Line 9 instructs the computer to print the string 'Hello World' on the screen.
To run the program, click the Run button at the top left. Or you can press the F9 key.
The output of the program appears in the bottom part of the window.
We see that the computer printed the string 'Hello World' and exited the program.
Instead of telling the whole World hello, let’s properly meet the user of our program.
We’ll first ask them what their name is. Then we’ll tell them that it is nice to meet them
and use their name.
CISB 11 Programming Lab
Edit line 9 so that the computer prints the string 'Hello, what is your name?' instead
of 'Hello World'
For our program to receive input, we use the function named input.
The input function will wait for the user to enter something.
Then it will take whatever the user entered and give it to our program as a string.
We will save that string in a variable and use it later.
A variable is some storage space in the computer’s memory that is dedicated for us to use
as temporary storage while our program is running.
We give variables names so that we can refer to their exact memory location when we want
to store or retrieve data there.
Let’s save the user’s input in a variable named userName
For line 10, we need to write the following code: userName = input()
The equals sign in this statement is the assignment operator. It instructs the computer
to take the data value from the right-hand side of the equals sign and save that value in the
variable whose name is on the left-hand side of the equals sign.
The assignment operator means something completely different from the equals sign that
we use in math class.
If you run the program now, you will see the prompt on the screen and the program waits
for you to enter something.
After you enter something, the program stores what you entered in the variable that we
named userName. Then the program is done and exits.
For the next version of our program, let’s use the userName variable and give a friendly
response to the user.
On line 11, we will write another print statement. This time, we need to use 2 arguments
for the print function.
The first argument is the string 'Nice to meet you,'
The second argument is the user’s name, which we stored in our username variable.
For line 11, we need to write the code: print('Nice to meet you,', userName)
The first comma is part of string for the first argument. The second comma separates the
first argument from the second argument.
Let’s test our code by running the program. After you see the computer ask you you’re your
name is, enter your name.
Make sure the computer tells you that it is nice to meet you and also prints your name.
CISB 11 Programming Lab
Finally, we will replace the comment at the beginning of the code with something more
informative.
Replace lines 3 through 6 with your name, the date, and “CISB 11, Mt. SAC” without the
quotes.
Your source code and test output should look very similar to this:
Save the file. Click the download button. On Canvas, on the page for this assignment,
submit your source code file.
You are a beginning Python programmer!
Download