Here are our solutions for the day 2 exercises in the 30 Days of Python series. Make sure you try the exercises yourself before checking out the solutions!
1) Ask the user for their name and age, assign theses values to two variables, and then print them.
First, let's focus on the user's name, since the approach is going to be very similar for their age.
In order to get a value from the user, we need to use the input
function, and it's good practice to provide a meaningful prompt, so that the user knows what we want.
input("Please enter your name: ")
Don't forget to put a space at the end of your prompt so that the user isn't writing directly next to the colon, or the last letter of your prompt text.
The next step is storing the user's response somewhere, so I'm going to assign the result of the input
call to a variable called name
:
name = input("Please enter your name: ")
We follow the same approach for the age:
name = input("Please enter your name: ")
age = input("Please enter your age: ")
Now we have our two variables, we can print the values by passing the variable names to print
, like so:
name = input("Please enter your name: ")
age = input("Please enter your age: ")
print(name)
print(age)
2) Investigate what happens when you try to assign a value to a variable that you've already defined. Try printing the variable before and after you reuse the name.
If you try this on your own, you should see that the variable's value gets replaced. For example, if we write this:
x = 5
x = 7
print(x)
The value we get printed out is 7
, not 5
. However, if we print x
before assigning a new value to this name, we get the original value:
x = 5
print(x)
x = 7
This is because Python is running our code top to bottom, so when we refer to x
when calling print
, we haven't yet updated the value of x
.
3) Below you’ll find some code with a number of errors. Try to go through the program line by line and fix the issues in the code.
The code we need to correct is this:
hourly_wage = input("Please enter your hourly wage: ')
prnt("Hourly wage: ")
print(hourlywage)
print("Hours worked: ")
print(hours_worked)
hours_worked = input("How many hours did you work this week? ")
There are a whole host of problems with this small piece of code, and I strongly recommend you make good use of the error messages Python provides to you to track them down.
On the first line we have an issue with the quotation marks.
hourly_wage = input("Please enter your hourly wage: ')
We start the string with double quotes, but we attempt to close off the string with a single quote. Quotes have to match, so this is invalid right now. We need to change it to this:
hourly_wage = input("Please enter your hourly wage: ")
A couple of lines down we have a misspelt reference to the print
function:
prnt("Hourly wage: ")
Instead of print
, we've accidentally written prnt
. In this instance, the error message is extremely helpful:
Traceback (most recent call last):
File "main.py", line 3, in <module>
prnt("Hourly wage:")
NameError: name 'prnt' is not defined
Once we fix this issue, we have another NameError
. We've forgotten the underscore in hourly_wage
here:
print(hourlywage)
It should be:
print(hourly_wage)
Finally, we have another NameError
, this one caused by the order of our code. We've defined hours_worked
after we've tried to use it.
print(hours_worked)
hours_worked = input("How many hours did you work this week? ")
Remember that Python is running our code top to bottom, so if it encounters a name before we've told it what it is, it's going to complain about undefined variables.
Moving the problematic line fixes the issue, and our fully working code might look like this:
hourly_wage = input("Please enter your hourly wage: ")
hours_worked = input("How many hours did you work this week? ")
print("Hourly wage: ")
print(hourly_wage)
print("Hours worked: ")
print(hours_worked)