Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing Data Types
#1
I am writing a program to change the given data type.
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 True
Output:
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
Reply
#2
input() returns a string. So dtype is always a string. Your error results from trying to convert a string with a decimal to int which it cannot do (at least as a string). You would either need to convert it to a float first, or parse out the decimal and after.
>>> int('12.5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12.5'
>>> int(12.5)
12
Any object that contains data is considered True, and if it is empty, is False. And string is just another object.
>>> a = ''
>>> bool(a)
False
>>> b = '0'
>>> bool(b)
True
>>> 
You would first have to convert the string '0' to a int 0 to be considered False.
>>> bool(int(b))
False
Recommended Tutorials:
Reply
#3
(Jun-27-2019, 12:43 PM)metulburr Wrote: input() returns a string. So dtype is always a string. Your error results from trying to convert a string with a decimal to int which it cannot do (at least as a string). You would either need to convert it to a float first, or parse out the decimal and after.
>>> int('12.5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12.5'
>>> int(12.5)
12
Any object that contains data is considered True, and if it is empty, is False. And string is just another object.
>>> a = ''
>>> bool(a)
False
>>> b = '0'
>>> bool(b)
True
>>> 
You would first have to convert the string '0' to a int 0 to be considered False.
>>> bool(int(b))
False
Thanks a lot for the explanation!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What data types can I use for default values? Mark17 1 522 Oct-09-2023, 02:07 PM
Last Post: buran
  In SQL Server, mix data types. shiv11 0 876 Sep-21-2022, 12:50 PM
Last Post: shiv11
  I need to add data types to cython conversion python to c Good_AI_User 1 997 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  Issue in changing data format (2 bytes) into a 16 bit data. GiggsB 11 2,644 Jul-25-2022, 03:19 PM
Last Post: deanhystad
  How to insert different types of data into a function DrData82 0 1,248 Feb-10-2022, 10:41 PM
Last Post: DrData82
  Why changing data in a copied list changes the original list? plumberpy 3 2,223 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  Creating Data Set for Changing Oscillation Motion jcraw77 0 1,584 Jun-19-2020, 12:25 AM
Last Post: jcraw77
  List of mixed data types to bytes medatib531 1 2,333 Mar-16-2020, 11:53 AM
Last Post: scidam
  Type error when reading in different data types on an __init__ method Dylanmull 3 2,780 May-09-2019, 02:05 PM
Last Post: buran
  Python validate excel values data types Useruser00 0 4,835 Apr-08-2019, 01:29 PM
Last Post: Useruser00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020