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
5 Réponses
+ 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)
+ 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'
0
hi,
try the code in sololearn python code generator.
please post the source.
0
Thanks for the help, that cleared things up
0
Anytime :)