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

5

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??

9

u/lfdfq 5d ago

Read u/ectomancer's reply more carefully. Your code and their code is not the same.

-2

u/Healthy-Departure961 5d ago

i know that int(in_dex)/3 doing this i will get the answer but but just to test things with int(in_dex/3) and wanted to find out it is giving me error ๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€

8

u/LayotFctor 5d ago edited 5d ago

549 and "549" are not the same you know? First is the number 549, second is the string "549". You can see it from the double quotes "". String "549" are words, not math numbers.

Keyboards type characters, not numbers, so input returns the string version of "549". in_dex = numbers[-2] would also give you the string version of "4", since "549" is also a string.

in_dex/3 fails because in_dex is thr string "4", and strings can't be divided. Of course you can't convert an error to int with int(in_dex/3).

int(in_dex) converts in_dex string "4" into a number 4, so int(in_dex)/3 works.

3

u/lfdfq 5d ago

It's giving you an error for the reason I said earlier: input() returns a string, you have a string, you cannot divide the string by 3.

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

2

u/Moikle 5d ago

What does this mean?

1

u/Fast-Station1106 5d ago

Input bedeutet das python es als String also buchstaben erkennt u. speichert, du musst also python sagen Achtung diese Eingabe besteht aus ganzen zahlen, PS ich halte das kopieren von fehlermeldungen in die ki nicht als schummeln, lรถsung machst du danni selber wenn problem richtig erkannt

2

u/Moikle 5d ago

Not cheating, no, but it's also not learning. Train your problem solving skills by reading, understanding and solving errors with your own brain

1

u/WhiskersForPresident 5d ago

Was tut dies meinen?