-1

I have posted data coming from a webform, and I am trying to convert lb's to oz's for shipping labels.

The webform is set to just post numbers.

<input type="number" step="1" name="lbs" id="lbs" class="form-control input-lg" placeholder="Lb's" tabindex="7">

Below is the python code

llb = request.form['lbs']
lw = (llb*16)
return lw

It always returns the wrong number.

2 for llb returns 2222222222222222 when it should be 32. I tried messing with 0o16 etc with no success.

I even tried making sure llb was an int with

llb = int(request.form['lbs']) 

I can't figure this out for the life of me. For all I know I could be over thinking this.

I even tried everything mentioned on Wrong math with Python? and a few others I could find simlar.

ajankuv
  • 499
  • 3
  • 22
  • `llb` is a string, not an integer. String multiplication means *repetition*. – Martijn Pieters Jan 10 '18 at 17:04
  • Add a `print(type(llb))` to make sure it's a `int` and not `str`. – liberforce Jan 10 '18 at 17:09
  • Again, please paste the result of `print (type(llb))`. And where does this `request` module comes from? I know `requests` or `urllib.request`, but that seems none of them. Please specify your version of python too. – liberforce Jan 11 '18 at 09:13

2 Answers2

1

Try:

llb = request.form['lbs']
lw = (int(llb)*16)
return lw

Since your request.form['lbs'] is str type you need to cast it to int type before multiplication. Multiplication sign * works with strings in such a way that it copies them given amount of times.

Example:

'^'*5
>> '^^^^^'

EDIT

Your problem seems to be that you have overwritten the built-in int with some other variable.

Example:

>>> lbs = '2'
>>> lbs
'2'
>>> lw = lbs*16
>>> lw
'2222222222222222'
>>> lw = int(lbs)*16
>>> lw
32
>>> int = 7
>>> lw = int(lbs)*16
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> del int
>>> lw = int(lbs)*16
>>> lw
32

You should never overwrite built-in names such as int, list, set, dict and so on: Why can you assign values to built-in functions in Python?

jo9k
  • 690
  • 6
  • 19
  • TypeError: 'int' object is not callable That is what i get with that. I had tried that earlier :/ – ajankuv Jan 10 '18 at 17:44
  • you must have overwritten `int` in your namespace. If you have any place in your code when you do something like `int = some_variable` or `int = 5`, you should change that `int` name to something other - you should never use built-in names for your variables. ==EDIT== Added example where I reproduce your problem. – jo9k Jan 11 '18 at 09:35
1

llb variable is a string. You need to cast it if you want an int.

'2'*16 will print 16 times the character 2.

If llb is supposed to be an int, just do:

lw = (int(llb)*16)

liberforce
  • 11,189
  • 37
  • 48
  • TypeError: 'int' object is not callable That is what i get with that. I had tried that earlier :/ – ajankuv Jan 10 '18 at 17:47