Python Rounding problems
HI guys,
I decided to write a calculator for Warehouselogistics and practice!
I set up a few vars and everything is working well so far. My only problem is, when it calculates 542 / 32 I get 16. It is 16,94 and should round up to 17.
I googled rounding in python and used int(round(x, 0)). ( x would be 16,94? ). i also tried just round(x) or round(x, 0), i thought for an half hour, but i just can't figure my fault out so i decided to post my problem here.
Edit: I wrote Import math on the top just to be sure If my full code is needed, let me know!
Edit: Could it be because I used variables for the calculation and not any number? I'm pretty new to Python so I don't know the most commands like "def" ^^
Thanks in Advance
ghxst wrote: PROBLEM: I set the numbers and vars to Int, Integer is for round numbers and not any decimals.
right?
b = 28 [ in this case, in my code it is raw_input('length? ') ]
Edit: L = 120 / b
l = 120 / b TypeError: unsupported operand type(s) for /: 'int' and 'str'
Solution: No clue, not yet!
b=float((raw_input("numbah: ")) L=120./b the decimal point after the 120 will make it a float. either way will do. only one of them needs to be for it to be evaluated as a float. int() will interpret a string as an integer
I decided to post my code, I can't figure it out myself :|
Task: 542cartons, length 28, broad 37, height 48, 20kg per carton and 230 cm height of the truck (all in cm)
original code
a = raw_input('How many cartons? ')
b=raw_input('Length? ')
c=raw_input('Broad? ')
d=raw_input('Height? ')
e=raw_input('Kg per carton? ')
f=raw_input('truck height? ')
l = int(120) / int(b)
ba = int(80) / int(c)
h = int(f) / int(d)
print l
print ba
print h
y = l*ba*h
print y
p = int(a) / int(y)
print p
raw_input('finished for now!')```
p outputs me 16
note: p should always be rounded up, i just remembered ^^
Look up the concept of integer division. That is what is happening to you. In many languages when you divide an integer by another int the answer is truncated to an integer. In the process of truncation the digits after the decimal are dropped completely, not rounded. You need to convert at least one of the ints to float.
Ex. In the shell entering 9/5 = 1. But entering 9/5.0 or 9.0 /5 = 1.8.
Also, this can depend on the version of Python that you have. Starting with version 3.1 and later, division using / will always give you the correct answer (9/5 = 1.8), and integer division is written as 9//5 = 1.
Hope this helps.
Wow, I can't believe it took that long for him to get a straightforward answer, guys.
@OP This was my first major issue with programming. When I first started with python (quite a while back), I pretty much almost gave up because I couldn't manage to get a simple interest calculator to work. I think whoever is writing python tutorials should make that stuff more clear for beginners >.>
also:
>>>542/32
16
>>>
>>>542.0/32.0
16.9375```
So:
```markup
float(542/32)
should be:
float(542)/float(32)
Also its a good idea to include a rounding digit; not needed, but good practice. example:
>>> round(542.0/32.0,0)
17.0
>>>
from there, you can convert to int or whatever.