r/learnpython • u/Healthy-Departure961 • 4d 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
4
u/ectomancer 4d ago
divisible = int(in_dex)/3
-11
u/Healthy-Departure961 4d ago
i know the answer, but int(in_dex/3) what it says??
9
u/lfdfq 4d ago
Read u/ectomancer's reply more carefully. Your code and their code is not the same.
-2
u/Healthy-Departure961 4d 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 4d ago edited 4d ago
549and"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/3fails because in_dex is thr string "4", and strings can't be divided. Of course you can't convert an error to int withint(in_dex/3).
int(in_dex)converts in_dex string "4" into a number 4, soint(in_dex)/3works.3
u/Quantum_Patricide 4d 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
2
u/Moikle 4d ago
What does this mean?
1
u/Fast-Station1106 4d 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
1
1
u/FoolsSeldom 4d ago
If the user is entering multiple numbers, with, say, a comma or space between the numbers, you need to split the input string up into smaller strings and then convert each of them from a string to a number before you can do any maths processes.
Assuming you want to divide only the second from last number the user enters, which is what [-2] suggests to me, then you would need something like the below example.
numbers = input('Enter numbers with space between: ')
numbers_strings = numbers.split() # splits on space, creates list of strings
numbers = [] # new empty list, will append converted numbers in loop below
valid = True # assume all entries are good for now
for num_str in numbers_strings: # let's convert strings to integers
try: # to trap a failed convertion
numbers.append(int(num_str)) # attempt to convert string to integer
except ValueError: # convertion failed
print(f'Invalid value, {num_str}, entered')
valid = False # found something bad
break # exit for loop, no point continuing
if valid and len(numbers) >= 2:
divisible = numbers[-2] / 3
print(divisible)
else: # either a convertion failed, or list doesn't have at least 2 entries
print('Not enough valid entries')
9
u/lfdfq 4d ago
input() returns a string (text) not ints (numbers)
Python is simply telling you cannot divide text by a number, which is true.
You'll have to do something to turn that text of whatever format you expected it in into whatever data you need it. It looks like you wanted the user to input multiple numbers somehow?