How to use input() with numbers?

QuestionsQuestions8 SkillsProYour First Python LabOct, 13 2025
094

To use the input() function with numbers in Python, you need to convert the input from a string to a numeric type (like int or float). Here's how you can do it:

Example with Integer:

number = int(input("Enter an integer: "))
print("You entered:", number)

Example with Float:

number = float(input("Enter a decimal number: "))
print("You entered:", number)

Explanation:

  1. Prompt: The string inside input() is displayed to ask the user for input.
  2. Conversion: The int() or float() function converts the input string to the desired numeric type.
  3. Variable Assignment: The converted number is stored in the variable number.

Note:

If the user enters a non-numeric value, it will raise a ValueError. You can handle this with a try-except block if needed.

If you have more questions or need further examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!