Learn how to get information from users and display results
The input() function lets your program ask questions and wait for the user to type an answer. It's like having a conversation with your computer!
# Ask the user for their name
name = input("What is your name? ")
print("Hello,", name, "!")
# Ask for their age
age = input("How old are you? ")
print("Wow,", age, "is a great age!") Try it! Type this code in Spyder and run it. The program will wait for you to type your answers!
There are many ways to make your output look nice and organized:
name = "Alex"
age = 12
print("Name:", name)
print("Age:", age)
print("Nice to meet you!", name) name = "Alex"
age = 12
print(f"Hi {name}!")
print(f"You are {age} years old")
print(f"Next year you'll be {age + 1}!") # Create a nice-looking profile
name = input("Enter your name: ")
hobby = input("What's your favorite hobby? ")
color = input("What's your favorite color? ")
print("=" * 30)
print(f" PROFILE: {name}")
print("=" * 30)
print(f"Hobby: {hobby}")
print(f"Color: {color}")
print("=" * 30)
When you use input(), Python treats everything as text. To do math, you need to convert it to numbers:
num1 = input("Enter first number: ") # User types "5"
num2 = input("Enter second number: ") # User types "3"
result = num1 + num2 # This gives "53", not 8!
print(f"The answer is: {result}") num1 = int(input("Enter first number: ")) # Convert to integer
num2 = int(input("Enter second number: ")) # Convert to integer
result = num1 + num2 # Now this gives 8!
print(f"Result: {result}") Create a program that asks for two numbers and shows their sum:
# Simple Calculator
print("=== SIMPLE CALCULATOR ===")
print()
# Get two numbers from the user
first_number = int(input("Enter the first number: "))
second_number = int(input("Enter the second number: "))
# Calculate the sum
total = first_number + second_number
print(f"The answer is {total}!")
print()
print(f"{first_number} + {second_number} = {total}")
print("Thanks for using the calculator!") Make a simple chatbot that asks questions and responds:
# Simple Chatbot
print("🤖 Hello! I'm a friendly chatbot!")
print()
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")
age = int(input("How old are you? "))
if age < 13:
print(f"You picked {age}! That's a great choice.")
elif age < 20:
print("You're a teenager! Cool!")
else:
print("You're an adult! Nice!")
favorite_animal = input("What's your favorite animal? ")
print(f"I love {favorite_animal}s too!") Challenge: Can you add more questions to make the chatbot even more interesting?
# Good: Clear instructions
name = input("Please enter your name: ")
# Better: Even clearer
name = input("What should I call you? ") # Use emojis and symbols
print("🎉 Welcome to my program! 🎉")
print("-" * 25)
print("✨ Let's get started! ✨")