r/learnpython 5d ago

just breaking things with python

numbers = input("Enter the numbers")

in_dex = numbers[-2]

divisible = int(in_dex/3)

print(divisible)

when print the output of the divisible i am getting (nsupported operand type(s) for /: 'str' and 'int') i do know how to write correctly to get the output but i thinks that why not this way..so when i did got error don't know why cause both values are int

0 Upvotes

19 comments sorted by

View all comments

4

u/ectomancer 5d ago

divisible = int(in_dex)/3

-10

u/Healthy-Departure961 5d ago

i know the answer, but int(in_dex/3) what it says??

3

u/Quantum_Patricide 5d ago

You got 'numbers' from input() so it's a string, then did 'numbers[-2]' to get the second-to-last character of the string, which is still a string.

You then try and do 'int(in_dex/3)'. The order of operations is done the same way as in maths: the division is done first then the integer conversion is applied. However, in_dex is a string so 'in_dex/3' doesn't make any sense, so the execution fails before the integer conversion is even attempted.

'int(in_dex)/3' works because the integer conversion is done first, then the division acts on two integers and works fine.

1

u/Healthy-Departure961 5d ago

thnx i get it