CMSC 201

Lab 2: A Simple Program

Input

What is it?

User input is a way to get information from the user (you!) after you've finished writing your program. It would be impossible to always know the exact data you'll need when you're writing your code.

How do I get it?

In Python user input looks like this:

name = input("Enter your name please: ")

When you run the finished program it will display something like this

Enter your name please: 

Casting:

In Python anything entered by the user is automatically turned into a string, but what if you need an int?

Python treats ints and strings differently so we need a way to tell the program the input is actually an int.
You can do this with a useful trick called casting. Since we know the user will input an int so you "cast" the input into an int variable.

age = int(input("Enter your age please"))