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

Error Trouble

I’ve recently started to learn python and have been playing around with it in the code playground, however I noticed that when I type: u=input() Entered number 60 u/=3 print(u) It produces an error: Traceback (most recent call last): File "..\Playground\", line 25, in <module> u/=3 TypeError: unsupported operand type(s) for /=: 'str' and 'int' I’m confused because I enter an integer and then divide by another integer, this works however when I first convert my input to a float, can someone explain

15th May 2018, 3:36 PM
Brenden Preiss
Brenden Preiss - avatar
5 Answers
+ 2
You see, when you input something to a program using input(), Python always treats it as a string. To convert input to an integer or float, use int() or float() respectively :) Eg: u = input() u = int(u) (or) u = float(u)
15th May 2018, 5:14 PM
Just A Rather Ridiculously Long Username
+ 1
input function takes an input and threats it as a string no matter what you type in. The 60 you typed in is a '60'
15th May 2018, 3:40 PM
Marcin Murzyn Wojnarowski
Marcin Murzyn Wojnarowski - avatar
0
hi, try the code in sololearn python code generator. please post the source.
15th May 2018, 3:44 PM
Rajeeb
0
Thanks for the help, that cleared things up
15th May 2018, 6:06 PM
Brenden Preiss
Brenden Preiss - avatar
0
Anytime :)
17th May 2018, 8:31 PM
Just A Rather Ridiculously Long Username