EXERCISE 3: Writing an Interactive Program With Input Prompts [Python3echo.py]

Purpose. The purpose of this exercise is to introduce the concept of input, in which a user of your program will type text in the output box in response to prompts that your program prints. This one adds an input statement, which youll use in several of the remaining exercises in this sequence.

This kind of program is called interactive because its input is supplied by a user, and not by the programmer (you). By contrast, the input you typed in exercise 2 is called hard-wired it was typed by the programmer (you), directly into the program.

Background. Just read -- your turn's coming! You learned in exercise 2 how to create a variable and store text in it. You also learned how to retrieve a copy of that text from the variable. But you have to 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 pull them from there. Then you can use the same program, without modification, with any set of input values.

Heres how its done. Instead of typing a value in quotes, use an input statement, like this: See image.

When you run, the program will pause there and show this in the output box do what it says: type your name, then press ENTER: See image.

If you use input("") more than once, it just goes through the prompts one at a time. See image.

Your Turn. 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. There are two variables (the winners name and the raffle prize). All the rest of the text in the print statements is boilerplate that is, its the same text for all prize winners.

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.

Step 1, write a full form letter without any variables, to print something like this:

print('Congratulations, DVC Student! You won our weekly computer science department raffle. Your prize is a live six-foot python. Please collect your prize at the division office before the end of the week')

Thats one very long line that the edit box automatically wraps for you, so no need to put in your own line breaks.

Run it and make sure it all works. Then apply one or both of the following options.

Step 2, Think up prompts that you want to use for getting input something like Enter the winners name or Who are you?. And Whats the prize? or The prize is.

Dont put these in the program yet just write them down someplace, so that when you need them in the Python program, theyre already thought out.

Step 3, Create a variables code block in the edit box. In it, #write input statements to get the name and prize using prompts. To make sure it works, put simple print statements at the end of the variables code block to print the two variables. If they are right, remove those print statements they were only to confirm things are working so far. See image.

Step 4, replace the winners name wherever it appears in the print statement. Replace it with the variable that stores the copy of the winners name as input. Something like

print('Congratulations,', winner, 'You…'). Make sure it works.

Step 5, replace the prize wherever it appears in the print statements, as you did the winner.

If you did this right, you will be able to change the winner and prize every time you run the program, without rewriting a single thing in the program itself!

Submitting Your Work. Submit your work as you did in previous exercises, submitting the contents of the repl.it edit box, or the PyCharm Python3echo.py renamed as Python3echo.txt. Heres a sample: See image.

HINT: Do not type all five input statements and then start typing the output statements. Instead type ONE input statement and see if it works that is, it runs and prompts you. THEN add a print statement to echo back what was entered make sure that works right. THEN go back and add the SECOND input statement, and fully test it before adding the third, and so on.

HINT: Avoid typing a variables name more than once. Copy/paste it instead. This avoids misspellings which lead to incorrect results or errors when the program runs.

HINT: Case matters in programming. myName with an uppercase N is not the same as myname with a lowercase n. Think of the alphabet as having 52 distinct characters a-z and A-Z instead of the usual 26.

EXERCISE 4: Doing Text Concatenation [Python4greeting.py]

Purpose. The purpose of this exercise is to show how text stored in variables can be concatenated, or joined, to form new text sequences. It allows you to control spacing!

This introduces a new code block that comes between the variables code block and the output code block. Its the calculations code block. It creates additional variables, but their values are not hardcoded, and not interactive (from input statements). They are calculated based on values stored in other variables.

Background. Heres how to concatenate variables containing text. Presuming that the variables myFirstName and myLastName already exist, we can create new variables like this: See image.

Then you can simply print one of these new variables, like this: print(myName).

Note that concatenation does not include spacing like the print statements with multiple, comma-separated values does. So the programmer has to put these in, like the text with one spacebar character separating first and last name above, and the space after Ms. above.

Your Turn. Rewrite the program from exercise 3 so that there are no print statements with multiple, comma-separated values. Create concatenated variables in a concatenated code block to combine into new variables whats in each of exercise 3s print statements. Even if a print statement has just one text value (like print('Congratulations!')), store that as a variable in the concatenation code block. Each print statement will print a variable, only.

Dont get the idea that print statements will always print just variables, and that text will always be concatenated for later printing thats not the case at all. Were just doing so here in order to show how concatenation works.

Submitting Your Work. Submit your work as you did in previous exercises, submitting the contents of the repl.it edit box, or the PyCharm Python4greeting.py renamed as Python4greeting.txt. Heres a sample of edit and output boxes with prompts: See image.

EXERCISE 6: Statistics Calculations Finding An Average [Python6Calc.py]

Purpose. The purpose of this exercise is to provide more practice in writing programs that (1) read numbers from an input file, (2) perform calculations, and (3) show output results.

Background. You know how to read multiple values from an input file. You know how to store them as numbers. You know how to addition, multiplication, and division. And you know how to echo input values and print calculated results. Thats all you need to know in order to do this exercise.

Your Turn. Write a program that reads five numbers from input prompts, adds them to get their total, and calculates their average by dividing their total by 5. For these numbers, use the present temperatures from 5 different cities in the world degrees C or F your choice. The output will look something like this: See image.

But wait lets do something about all those zeros! Heres how to round off a number to a maximum of two decimal digits, using a variable name average for storing the calculated average:

average = '%.2f' % average

Statements like this belong in the calculations code block, after the variable average gets calculated and before average gets printed in the output code block, like this:

sum = a + b + c + d + e
average = sum / 5
average = '%.2f' % average

This is the first time were seeing a calculation that has the same variable name appearing twice. In this case, the original number stored in average (in this case, 73.96000000000001) gets replaced with an updated, rounded-off value (in this case, it will be 73.96). Apply this, so that your program shows something like this: See image.

Submitting Your Work. Submit your work as you did in previous exercises, submitting the contents of the repl.it edit box, or the PyCharm Python6Calc.py renamed as Python6Calc.txt.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.