OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why in Python can I convert 'str' to decimal but not convert a decimal in a 'str' to an integer?

  • Thread starter Thread starter Arthur Fleming Santos.
  • Start date Start date
A

Arthur Fleming Santos.

Guest
I was creating a Python code that had, in the first input line, an "input()" for the user to enter an integer, then the code would inform whether this number was even or odd. My code:

Code:
num_str = input('Whole number: ')

try:
    num_int = int(num_str)
    if num_int % 2 == 0:
        print(f'{num_int} is an even number.')
    else:
        print(f'{num_int} is an odd number.')

except:
    print(f'"{num_str}" is not a whole number!')

There comes a point where I need to convert the type of the variable num_str, which is of type 'str', to type 'int'. If this conversion generated a type error, the error would be treated with 'except' (in the case of the user entering text or float, for example). So I wonder: Why can Python convert a decimal number, stored as a string, to an integer, but cannot convert an integer, also as a string, to decimal?

Code 1: entering an integer

Code:
a = str(input('Enter a number: '))

int(a)

Output:

Code:
>>>Enter a number: 15
>>>15.0
>>>

Code 2: entering a decimal number

Code:
a = str(input('Enter a number: '))
print(int(a))

Output:

Code:
>>>Enter a number: 15.5
>>>Traceback (most recent call last):

  File "C:/Users/Arthur Fleming/OneDrive/Área de Trabalho/test2.py", line 2, in <module>

    print(int(a))

ValueError: invalid literal for int() with base 10: '15.5'

See that I'm not converting type 'float' to 'int' or vice versa. And yes, actually trying to transform a decimal number, which is a string, into an integer!
<p>I was creating a Python code that had, in the first input line, an "input()" for the user to enter an integer, then the code would inform whether this number was even or odd.
My code:</p>
<pre><code>num_str = input('Whole number: ')

try:
num_int = int(num_str)
if num_int % 2 == 0:
print(f'{num_int} is an even number.')
else:
print(f'{num_int} is an odd number.')

except:
print(f'"{num_str}" is not a whole number!')
</code></pre>
<p>There comes a point where I need to convert the type of the variable <code>num_str</code>, which is of type 'str', to type 'int'. If this conversion generated a type error, the error would be treated with '<code>except</code>' (in the case of the user entering text or float, for example).
So I wonder: Why can Python convert a decimal number, stored as a string, to an integer, but cannot convert an integer, also as a string, to decimal?</p>
<p>Code 1: entering an integer</p>
<pre><code>a = str(input('Enter a number: '))

int(a)
</code></pre>
<p>Output:</p>
<pre><code>>>>Enter a number: 15
>>>15.0
>>>
</code></pre>
<p>Code 2: entering a decimal number</p>
<pre><code>a = str(input('Enter a number: '))
print(int(a))
</code></pre>
<p>Output:</p>
<pre><code>>>>Enter a number: 15.5
>>>Traceback (most recent call last):

File "C:/Users/Arthur Fleming/OneDrive/Área de Trabalho/test2.py", line 2, in <module>

print(int(a))

ValueError: invalid literal for int() with base 10: '15.5'
</code></pre>
<p>See that I'm not converting type 'float' to 'int' or vice versa. And yes, actually trying to transform a decimal number, which is a string, into an integer!</p>
 

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top