Your goal in this lab is to create a program that can say hello to the user with their own name.
You will be taking everything you learned in the last lab about GitHub, VS Code and Python and building on it.
Open GitHub Desktop and the Mad Libs repository
Open the repository in VS Code (or Codespaces)
Open lab2.py
Variables in programming act like containers or storage boxes that hold data. You give each box a name so you can find it easily, and you can put different types of information inside, such as numbers, text, or even lists of items. Once a variable is created, you can use it in your program to perform operations, make decisions, or display the stored information.
x = 10 # x is a variable storing the integer 10
name = "Alice" # name is a variable storing the string "Alice"
Check out below to see how I've created a variable in Python, you can also run the code to see what happens.
The input() function in Python allows you to accept text input from the user.
The function reads a line from the input (usually the keyboard), converts it into a string, and returns it.
The basic syntax for input() is: input(prompt)
The prompt is optional (but recommended) - a string that is displayed to the user, guiding them on what to input.
This word "string" keeps popping up. A string is a sequence of characters enclosed in quotes. In Python, you can use either single (' ') or double (" ") quotes to create strings. Strings can contain letters, numbers, symbols, and spaces. For example, "Hello", '123', and "A+B=C" are all strings.
String concatenation is the process of joining two or more strings together to create a new string.
In Python, you can concatenate strings using the + operator. Notice the print() function will also concatenate strings natively (including a space) if given multiple arguments (the things in the parenthesis).
Using what you have learned, create a program that asks for a name and then greets the user by that name
It should closely resemble the interactive example below
Save all of your work and close VS Code
Back in GitHub Desktop, commit with a meaningful comment. E.g. "Finished lab 2"
Push your code
Congratulations on finishing Lab 2, you can now:
Create variables and store information in them
Get input from the user
Join strings using concatenation
Once again, from Harvard's CS50P if you feel like you need further explanation or have missed a lesson: