Python: Capturing and Formatting User Input

Go to Chapter 23 in A Smarter Way.

You can ask for user input and put it into a variable in one line:

user_age = input("Enter your age: ")
print(user_age)

Anything the user enters will be treated as a string. Sometimes that’s a problem, since you may need to do math with their input.

age_integer = int(user_age)

To convert the string to a decimal value (a float):

age_float = float(user_age)

If you  need to convert a number back into a string:

age_float = str(age_float)

Exercises

See http://www.asmarterwaytolearn.com/python/23.html

See http://introtopython.org/while_input.html#Accepting-user-input

  1. Create a new code block in your examples.py file.
  2. Request the user’s name and age (in other words make two requests).
  3. Format the inputs properly to print them in a line that contains the user’s name followed by their age.