Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python Lesson 3

Uploaded by

experimentprueba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Lesson 3

Uploaded by

experimentprueba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming Exercise 3: Writing an

“Interactive Program” with Input


“Prompts” And Using Concatenation
[Last_First_prize.txt]
Review. In the second exercise you were introduced to code blocks and variables.
You learned in exercise 2 how to define a variable and store text in it. You also learned
how to retrieve a copy of that text from the variable with the print function. But with
the code you wrote in exercise 2,you must edit the program every time you want to
change any of the values you stored in the variables. It would be nice if you could
type those values in response to a “prompt” and get your program to store and use
what you (or any other user of your program) input(s) in response to the prompt. Then
you can use the same program, without modification, with any set of input values.

Purpose. The purpose of this exercise is to introduce the concept, input, in which a
user of your program types a response to “prompts” that your program displays, and to
show how text stored in variables can be concatenated, or joined, to form new text
sequences which allow you to control spacing in the output.

Input functions. First, let’s learn about the input function.

This kind of program is called “interactive” because its input is supplied by a user, and
not by the programmer. By contrast, the input you typed in exercise 2 is called “hard-
coded” – it was typed by you as the programmer directly into the program code using
an assignment statement at the same time that you defined the variable.

Here’s how it looked in the second exercise:

Now, instead of typing a value in quotes, you will use an “input” function, like this:
Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

When you run it, the program will pause


and show the prompt in the console:

Now, do what the prompt says: In the console, click once after the prompt, type
your name, then press [ENTER]:

If you use the input function more than once like this:

it just goes through the prompts one at a time.

Now it's your turn! The application for this exercise is to write a program that produces
a form letter, informing a raffle winner of their prize.

Develop this program “organically”, step-by-step. Remember that in the organic


approach, the program starts from very simple beginnings – usually a
copy/paste/markup of a previous working program. Then add features one-by-one,
testing and confirming each change. Here are the steps to write the program for
this exercise, using this technique.

REMEMBER THE HINT: Avoid typing a variable’s name more than once. Use the drop-
down list in the program editor or copy/paste it, instead. This avoids misspellings which

Prof. Strait Python Lesson 3.docx Page 2 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

lead to incorrect results or errors when the program runs.

Remember to open up your text editor to keep a copy of your code before you start
coding in the repl.it python 3 program editor.

STEP 1. DO THIS:

 Retrieve a copy of your code from Exercise 2.

If you uploaded a copy to OneDrive, download it to your computer from OneDrive.


If you didn’t save a copy in your OneDrive, you can download it to your computer
from Canvas.

 Open the file with the source code from the second exercise into your text
editor and save a copy with the file name Last_First_prize.txt.
 Copy and paste the source code from the first exercise into the program
editor at repl.it
 Run the program to make sure it still works.
 Delete all but the first 5 lines from your previous exercise. The first five lines
include the three comment lines, a blank line 4, and the comment line
labeling the variable code block on line 5.
 Change the Exercise number to 3
 Run your program again to make sure there are no errors.

There will not be any output because you have no print statements in your code yet
but run the program anyway just to make sure there are no errors in the first five lines
you have.

Writing input functions.


Look again at the example input functions above for firstName and lastName. The
part inside the parentheses and quote marks is the prompt. This is not the input. This is
what will be displayed on the screen in the console to prompt the program user (you
in this case) to enter some input.

Think up prompts that you want to use for getting input – something like “Enter the
winner’s name: ” or “Who are you? ”. And “What’s the prize? ” or “The prize is ”.

Notice the space between the last text character in each prompt and the ending
quote mark. This extra space is included so that a space will appear between the
prompt displayed in the console and whatever is typed by the user in response to the

Prof. Strait Python Lesson 3.docx Page 3 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

prompt.

Don’t put these in the program yet – just write them down someplace, so that when
you need them in the Python program, they’re already thought out.

STEP 2. DO THIS:

 In the variables code block beneath the comment line on line 6, type input
functions to get the winner’s first name, winner’s last name, and raffle prize,
using your prompts.
 To make sure it works, create an output code block and insert simple print
statements to print the three variables, and run your program.

Here is an example:
When you run the program, you will see only the first prompt in the console.

You must type something in response to the prompt for the program to continue.
Click once where you see the rectangle shape at the end of the prompt, and then
type your response. In my example, I type the first name Lisa, and press the Enter key.
You see in the screenshot below that as soon as I press the Enter key after typing a
first name, the running program displays the second prompt and pauses for the user
to respond.

Prof. Strait Python Lesson 3.docx Page 4 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

I click on the rectangle at the end of the second prompt, type a last name, Smith,
and press the Enter key again. The program then continues and displays the third
prompt.

I click on the rectangle at the end of the third prompt and type a prize and press
Enter again.

Prof. Strait Python Lesson 3.docx Page 5 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

The program then completes by displaying the output of the three print functions.

STEP 3. DO THIS:

 After you get your input functions right, delete


the variable names from the 3 print
statements you added to the output code
block, so that only the print commands and
the parentheses remain, like so:

STEP 4. DO THIS:

 Construct the first new print statement on line


11 by inserting the text, “Congratulations,”
followed by a comma, then your winnerFirstName variable, another comma,
then your winnerLastName variable, followed by another comma, and an
exclamation point enclosed in quotes.

It should look like this:

Prof. Strait Python Lesson 3.docx Page 6 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

Note that the first comma is part of the text string: “Congratulations,”. The second and third and
fourth commas are delimiters separating the first text string from the first variable name and the
first variable name from the second variable name and the second variable name from the last
text string, respectively.

 Run the program to make sure it works.

STEP 5. DO THIS:

 Construct the second print statement on line 12 that outputs this text: “You won
the raffle.”
 Run the program again to make sure it works.
 Construct the third print statement on line 13 that includes the text “Your prize
is”, followed by a comma, then the rafflePrize variable, followed by another
comma, and then a period enclosed in quotes.
 Run the program again to make sure it works.
 Insert a fourth print statement that outputs this text: “You will get an email
about how to collect your prize.”
 Run the program again to make sure it works.

After you run the program and enter input, your code and output should look
something like this:

If you did this right, you should be able to change the winner and prize every time

Prof. Strait Python Lesson 3.docx Page 7 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

you run the program, without rewriting a single thing in the program itself!

Concatenation. Now let’s learn about concatenation.

If your code included the punctuation as shown in the example code above, your
output would have looked something like this:

Notice the extra spaces in the output indicated by the red arrows in this example of
the output. Did you notice extra spaces in your output like these?

Those extra spaces were inserted in the output by the computer when you used the
comma delimiter between the text strings and variable names in your print statements.

Using concatenation, you can eliminate those extra spaces and control the spacing in
your output.

The calculations code block. The next step introduces a new code block that comes
between the variables code block and the output code block. It’s the “calculations
code block”. This is where you can create additional variables, that do not have hard-
coded values, or values acquired interactively from input functions. The values of
these variables are calculated based on values stored in other variables.

Concatenation is one kind of “calculation” you can do to derive values for new
variables. In this exercise you are going to concatenate (join) the contents of two
variables to get the value of a new variable.

You are going to concatenate or join the contents of variables winnerFirstName and
winnerLastName into one new variable, winnerName, like this:

The new variable, winnerName, is calculated by joining the content of the variable
winnerFirstName, a space character (shown in the example as an empty space

Prof. Strait Python Lesson 3.docx Page 8 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

enclosed in single quotes), and the content of winnerLastName. The plus sign is used as
the concatenation operator in Python.

Then you can simply print the new variable, winnerName, rather than the two
separate variables used to get its value, and the output will be the first name and last
name separated by one space. If you did not include the space character in the new
variable, the first name and last name would be output together without being
separated by a space.

Concatenation does not include default spacing like the print statements you wrote
before with multiple, comma-separated values does. The programmer must add
required spaces as part of the text string. You will encounter many more cases where
you must add the spaces in this and future lessons.

Also, note the symbol used for concatenation is the plus sign, “+”, the same symbol
used for addition when adding numbers together. We will discuss the difference
between joining text and adding numbers in the next lesson. For now, just be mindful
that you are using the plus sign to join text strings together; you are not adding in the
math sense.

STEP 6. DO THIS:

 Between your variables code block and your output code block, enter a new
line. Leave line 9 blank and on line 10 enter a comment line as a label for the
new calculations code block.
 After the new calculations code block comment line, create a variable named
winnerName to concatenate the first and last names that appear in your first
print statement, like the example given above.
 In the output code block, replace the winnerFirstName and winnerLastName
variables in your first print statement with the new concatenated variable
winnerName and run your program to test your code changes.
 Leave line 12 blank.
 Run the program again to make sure it works.

Now your input and output should look something like this (with your prompts and inputs, of
course):

Prof. Strait Python Lesson 3.docx Page 9 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

STEP 7. DO THIS:

 Next, back in the calculations code block, on line 12, create a new variable
named congratulateWinner to concatenate the winnerName variable just
created with the text in the first and second print statements.

In this step, you are concatenating the contents of the winnerName variable with two
lines of text that you want to still be output in two lines without having to use two print
statements. To accomplish this, include what is called a new line control character in
the text string.
(1)
It should look like this:

(2) (3)

In creating this concatenated variable, you are controlling spacing in your output in
three different ways: (1) adding a needed space between words, (2) removing an
unwanted space between words, and (3) using a new line control character to force
the output to print as two lines.

 In the output code block, replace your first two print statements with the single
print statement print(congratulateWinner) and test your program.

Now your input and output should look something like this:

Prof. Strait Python Lesson 3.docx Page 10 of 11


Programming Exercise 3: Writing an “Interactive Program” with Input “Prompts” [prize]

STEP 8. DO THIS:

This step is a repeat of what you just did in steps 6 and 7, so consult the examples
above if you need help.

 In the calculations code block, create another new variable named


announcePrize to concatenate the third and fourth print statements.
 In the output code block, replace the last two print statements with the single
print statement print(announcePrize) and test your program.

I am not providing a sample of the completed code for this lesson. If you understand
what you did in Steps 6 and 7, where I did provide sample code, you should have no
problem finishing Step 8 without a sample.

Your output should look the same as it did after step 6, except the unwanted space
between the content of the rafflePrize variable and the period ending the sentence
will be gone.

You will have three concatenated variables in your calculations code block, and only
two print statements in your output code block.

Submit only your program code text file, Last_First_prize.txt, for lesson 3.

Prof. Strait Python Lesson 3.docx Page 11 of 11

You might also like