Back to Python Mastery
BeginnerChildren
Basic Input and Output
So far, our programs have been a bit predictable. Let's make them interactive! We can ask the user for information using the
When you use
In this example, the program prints "What is your name? ", waits for an answer, and then greets the user by the name they entered.
The
Here,
input() function.When you use
input(), the program will pause and wait for the user to type something and press Enter.name = input("What is your name? ")print(f"Hello, {name}!")In this example, the program prints "What is your name? ", waits for an answer, and then greets the user by the name they entered.
The
input() function always gives you back a string. If you need to work with a number, you have to convert it.age_string = input("How old are you? ")age = int(age_string)next_year_age = age + 1print(f"Next year, you will be {next_year_age}!")Here,
int() converts the string from the input into an integer (a whole number).Greeting Machine
Ask the user for their name and then print a greeting message. This exercise cannot be auto-graded due to its interactive nature.