Skip to content

Latest commit

 

History

History
30 lines (24 loc) · 1.37 KB

File metadata and controls

30 lines (24 loc) · 1.37 KB

Let me know which area you are interested in, and I can provide a suitable example! Here's a simple Python example that demonstrates how to get input from a user. This code will prompt the user to enter their name and age, and then it will print a greeting message using the provided information.

# Get user's name
name = input("Enter your name: ")

# Get user's age
age_input = input("Enter your age: ")

# Convert the age to an integer
try:
    age = int(age_input)
except ValueError:
    print("Invalid age entered. Please enter a numeric value.")
    age = None

# Print a greeting message if age is valid
if age is not None:
    print(f"Hello, {name}! You are {age} years old.")

How It Works

  • The input() function is used to gather input from the user. It always returns data as a string.
  • The user's name is stored as a string in the name variable.
  • The user's age is initially read as a string in the age_input variable. We then attempt to convert it to an integer using int().
  • A try-except block is used to catch any ValueError in case the conversion fails (e.g., if the user enters non-numeric input).
  • If the age is valid (converted successfully to an integer), a greeting message is printed. Otherwise, an error message is displayed.

This is a basic example, but it shows how to handle user input and provides a foundation for more complex interactions.