Jun-27-2019, 12:07 PM
(This post was last modified: Jun-27-2019, 12:07 PM by BallisticSwami.)
I am writing a program to change the given data type.
Here is my code:
PS. Changing line 4 to:
Here is my code:
a = input("Type the data which you would like to convert: ") dtype = input("Type the data type which you would like to convert it to: ").lower() if dtype == "integer" or dtype == "int": a = int(a) print(f"The data converted to {dtype}: {a}") elif dtype == "boolean" or dtype == "bool": a = bool(a) print(f"The data converted to {dtype}: {a}") elif dtype == "floating" or dtype == "float": a = float(a) print(f"The data converted to {dtype}: {a}") elif dtype == "string" or dtype == "str": a = str(a) print(f"The data converted to {dtype}: {a}") elif dtype == "complex": a = complex(a) print(f"The data converted to {dtype}: {a}") else: print("Invalid Input") print(type(a))This is the error I get when I try to convert float to integer:
Error: File "grp_tasks.py", line 25, in <module>
a = int(a)
ValueError: invalid literal for int() with base 10: '12.5'
Also when I input "0" and try to convert it to boolean. It shows up as TrueOutput:Type the data which you would like to convert: 0
Type the data type which you would like to convert it to: boolean
The data converted to boolean: True
What is wrong with my code?PS. Changing line 4 to:
a = int(round(a))solves the error