Share your repls and programming experiences

← Back to all posts
4.2 Exercise B
MichaelAlfano (0)

Assignment:

Input, Variables and Typecasting
Along with everything above, you are also going to figure out how getting user input works in this activity. When you are complete, submit your program for review.

Create new Python Repl.it called, "Variables and User Input".
Add you name, date and assignment name at the top using comments.
Enter the following line of code underneath the comments: name = input("Please enter your name: ").
What is the value of name after this line has executed?
Add a print function afterwards saying hello to the person who just entered their name - use concatenation to join together their name and your salutation. Example: print("Welcome " + name + "."). This statement will display the value of name.
Run and test your program - fixing any errors so that it executes as expected.
Prompt the user for another input, this time asking them for their age.
Use a line similar to: age = input("How old are you? ")
On the next line, take the age the user entered and add 10 to it: age = age + 10
After that line, print back to the user with an output that uses both the name and age variables and tells the user how old they will be in 10 years.
Run and test your program... uh oh. Error?
Go back to the line where you got your input for age and typecast the input as an int: age = int(input("How old are you? "))
Now run and test your program again... uh oh... another error?
Finally, go to the print statement and typecast the variable - age - as a string. Your output should look like:
print(name + ", you will be " + str(age) + " years old in 10 years.")

Add at least two comments in the program documenting for what the program is doing, changes that you made in order to make it unique or as important notes to yourself about what you learned.