Python: Operand Error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Python: Operand Error

I get this error: File "/storage/emulated/0/Test.py", line 14, in <module> print ("is: "), ("num1" + "num2" + "num3" / 3) TypeError: unsupported operand type(s) for /: 'str' and 'int' When I run this program: num1 = '90' num2 = '87' num3 = '82' print ("The average of the following numbers: ") print (num1) print (num2) print (num3) print ("is: "), ("num1" + "num2" + "num3" / 3) Any ideas? Been a while since I last programmed.. Thanks :)

23rd Jul 2018, 4:38 AM
Heinsoft
Heinsoft - avatar
8 Answers
+ 3
This works: num1 = 90 num2 = 87 num3 = 82 print("The average of the following numbers: ) print(num1) print(num2) print(num3) print("is:", (num1 + num2 + num3) / 3) https://code.sololearn.com/cVKjtQ9Su3lB/?ref=app The best way to get help with a code is to save it in the playground and post a link here (3 dots connected by 2 lines > copy to clipboard > paste here) (or hit the ⊕ here > choose Insert Code > My Codes (from the drop down menu)) 🙂
23rd Jul 2018, 6:00 AM
David Ashton
David Ashton - avatar
+ 1
First, you should not put variable names in quotes. Now you are concatenating name strings, get the string "num1num2num3" and are trying to delete it by 3 - and of course you can't. But even if you'll remove the quotes in last line you'll still get the same error. Because you initialize variables with numbers in quotes - so you don't get number e.g. 90, but a string "90". So in the last line you get the string " 908782" and are trying to divide it by three.
23rd Jul 2018, 4:56 AM
Дмитро Іванов
Дмитро Іванов - avatar
+ 1
A, hell, there's also a crazy print call with two brackets in a row. You can't call functions like this, never. All arguments should be inside one exterior brackets. You should do that like this print("is: "+((num1+num2+num3)/3)) And, as you could have noticed, you should put sum in brackets, because otherwise you are adding num1, num2 and num3/3 and that, obviously is not what you want.
23rd Jul 2018, 5:07 AM
Дмитро Іванов
Дмитро Іванов - avatar
+ 1
Will do that next time, my apologies! It's working great now, thanks! :)
23rd Jul 2018, 6:05 AM
Heinsoft
Heinsoft - avatar
0
Still getting the same error after removing those quotes, anyways thanks for the heads up! What else am I missing here? Thank you :)
23rd Jul 2018, 5:02 AM
Heinsoft
Heinsoft - avatar
0
Cool! I remember seeing those brackets, thanks! I recalled it now, but one last issue though, EOF parse error. Perhaps it is with the int problem?
23rd Jul 2018, 5:21 AM
Heinsoft
Heinsoft - avatar
0
Ah, yes, you cant just concatenate an int to string, that's not js You should do print("is: "+str((num1+num2+num3)/3)) or print("is:, end=" "); print((num1+num2+num3)/3)
23rd Jul 2018, 5:28 AM
Дмитро Іванов
Дмитро Іванов - avatar
0
Nice, getting there. Now I have this program: num1 = '90' num2 = '87' num3 = '82' print ("The average of the following numbers: ") print (num1) print (num2) print (num3) print("is:, end=" "); print ((num1+num2+num3)/3) Then getting an error: File "/storage/emulated/0/Test.py", line 14 print("is:, end=" "); ^ Thanks for the effort though :)
23rd Jul 2018, 5:55 AM
Heinsoft
Heinsoft - avatar