📘
Type Converter
EasyPython Basics
Problem Statement
Convert values between different Python types.
Background
Python provides built-in functions for type conversion:
- int(x) - convert to integer
- float(x) - convert to float
- str(x) - convert to string
- bool(x) - convert to boolean
Your Task
Write a function convert_value(value, target_type) that converts the value to the specified type.
Input
- value: Any Python value
- target_type: String indicating target type ("int", "float", "str", "bool")
Output Format
Return the converted value, or "Error" if conversion fails.
Example:
Input:
value = "42", target_type = "int"
Output:
42
Reasoning:
int("42") converts the string "42" to integer 42
Constraints:
- Handle conversion errors gracefully
- target_type will be one of: "int", "float", "str", "bool"
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.