Program Overview
This Python program reads three values from the user and detects the data type of each one.
It first tries to convert each value to an integer or a float; if neither conversion works, the value is treated as a string.
Python Code:
# Read three values from the user
val1 = input("Enter the first value: ")
val2 = input("Enter the second value: ")
val3 = input("Enter the third value: ")
# Helper that detects the most specific type of a value
def detect_type(value):
try:
return type(int(value))
except ValueError:
try:
return type(float(value))
except ValueError:
return type(value)
# Display the detected types
print("\n--- Result ---")
print(f"Type of the first value: {detect_type(val1).__name__}")
print(f"Type of the second value: {detect_type(val2).__name__}")
print(f"Type of the third value: {detect_type(val3).__name__}")Sample Output:
Enter the first value: 42
Enter the second value: 3.14
Enter the third value: hello
--- Result ---
Type of the first value: int
Type of the second value: float
Type of the third value: strStep-by-Step Explanation:
- Three values are read from the user
- The program tries to convert each value to an integer, then to a float
- If both conversions fail, the value is treated as a string
- The type name is shown using __name__