Interacting with the User

greetings.py: 4 points
fancy.py: 8 points


Greetings!

greetings.py will be the first program in which we get input from the user. Start by clicking on the greetings.py file on the left-hand panel of Codespaces.

To get input from the user, the first instruction we’ll need is the following. Note that "Enter your name" is a prompt for the user - you should not replace it with your own name!

name = input("Enter your name: ")

This is an assignment statement. The expression on the right-hand side uses a built-in python function called input() to get a string from the user. When the program gets to this statement, it will print the string in the parentheses and wait for the user to type something and press enter. Whatever the user types before pressing the enter key will be returned and stored in the variable name. After the value is assigned, you should print a greeting with a statement such as:

print("So, we meet again,", name, "!")

For example, if we run our program by typing in at the Terminal,

python3 greetings.py

and type in an input of Yeobie, then the string "Yeobie" will be stored in the variable name and we should see the following output:

So, we meet again, Yeobie !

Notice that in the expression

print("So, we meet again,", name, "!")

that we didn’t need to add a space before the user’s name to the string "So, we meet again,". Instead, Python automatically replaces each separating comma that occurs outside of a string in the print() statement with a space. Those commas are used to list all of the things that we want Python to print for us. Since there were two separating commas used here (the first between "So, we meet again," and the variable name, the second between the variable name and "!", a space was automatically added before and after the user’s name. That means that Python automatically inserted a space before the exclamation point… but we’ll fix that in our next program.

Fancier Greetings!

Next, we will create a program called fancy.py that prompts the user to enter three strings, one at a time, in the following order: (1) first name, (2) last name, and (3) nickname.

The program should then print “Welcome back, first name *nickname* last name!” For example, assuming I enter my info correctly, it should print:

Welcome back, Yeobie *The Magnificent* Squirrel!

To remove the space before and after the nickname, we will create the entire string using string concatenation before we pass it into the print() function. String concatenation uses the + operator to add together two strings. For example,

s = "Hello" + "CSCI 150"

will result in the string s holding the value "HelloCSCI 150". For more on string concatenation, please see your Warmup. You should use string concatenation to create a single string with the correctly formatted version of what you want to print, then pass that string into the print function.

Once you’ve finished your program, don’t forget to commit and push your code before you move onto the next part of the lab. Instructions on how to do so can be found in the Warmup