User Input

In this section, we'll talk about making our program interact with an outside user.

What is User Input?

There are many data types in Python. For this section, we'll be focusing on String, Integers, and Floats.

Syntax

When we want the user to input some information, we can write 'input(prompt)'. The input will let the user input something, and the prompt is left for you to fill in with text. The user-input will always come in as a string.

Example: 

x = input("Enter your name:")
print("Hello, " + x)

In this example, we declared the variable 'x' to take on the string value of the user input. Then, we print "Hello", followed by the user-input. 

Integer Input

We know that User-Input will always come in as a string. But what if we want to prompt for a number, and do calculations with these numbers? We simply need to cover the input prompt with the word "int"

Example: 

x = int(input("Enter your age:"))

print(x)

 

In this example, we declared the variable 'x' to take on the integer value of the users age. Notice that the input prompt is written the same way, but we cover it with int() to signify that we are treating this input as an integer value. What will the program 

Another Method

Another way we can take integer input is by taking the input as a string first, and then making a separate variable to take the value as an integer. This is helpful when we want to keep the variables separate.

Example:

x = input("Enter your age:")

val = int(x)

print(val)

Exercises:

1. Prompt the user for their age, and print out 5 times this age.

2. Suppose the user inputs 2 Strings. Print the concatenation of these, with a space in between.