Follow us on Twitter!
The measure of a mans life is not how well he dies, but how well he lives.
Friday, April 25, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 18
Guests Online: 16
Members Online: 2

Registered Members: 82909
Newest Member: awais
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Python Rounding problems

ghxst
Member



Posts: 25
Location:
Joined: 12.05.10
Rank:
Newbie
Posted on 20-07-10 09:37
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


img819.imageshack.us/img819/5121/hbhsig.png

Edited by ghxst on 20-07-10 09:47
Author

RE: Python Rounding problems

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Moderate
Posted on 20-07-10 09:42
Python doesn't like decimals. Try using the math lib, or other built in libs for decimals and the likes. Trust me, you just need to look harder.



www.userbars.com/74460/665255/337-7865-ubda3219.gif
kbcarte.wordpress.com
Author

RE: Python Rounding problems

ghxst
Member



Posts: 25
Location:
Joined: 12.05.10
Rank:
Newbie
Posted on 20-07-10 09:51
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? 'Wink ]

Edit: L = 120 / b

l = 120 / b
TypeError: unsupported operand type(s) for /: 'int' and 'str'




Solution: No clue, not yet!


img819.imageshack.us/img819/5121/hbhsig.png

Edited by ghxst on 20-07-10 09:59
Author

RE: Python Rounding problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-07-10 12:44
ghxst wrote:
PROBLEM: I set the numbers and vars to Int

Why not try setting them to float? Just a guess.


Author

RE: Python Rounding problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-07-10 13:02
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? 'Wink ]

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: "Wink)
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


Author

RE: Python Rounding problems

ghxst
Member



Posts: 25
Location:
Joined: 12.05.10
Rank:
Newbie
Posted on 20-07-10 13:16
It seems that Python thinks that 542/32 is 16. I typed just 542/32 into the shell and it output me 16.

I also typed float(542/32) output -> 16.0

round(542/32) -> 16.0

Did i do something wrong?

Again I'm pretty new to Python, just trying every command until I find the one that works ^^


img819.imageshack.us/img819/5121/hbhsig.png

Edited by ghxst on 20-07-10 13:37
Author

RE: Python Rounding problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 20-07-10 13:26
ghxst wrote:
I also typed float(542/32) output -> 16.0

Well, if you make an int (something that is only 16) into a float then obviously it won't go much higher than 16.0. Float is to be decided before division.


Author

RE: Python Rounding problems

ghxst
Member



Posts: 25
Location:
Joined: 12.05.10
Rank:
Newbie
Posted on 20-07-10 13:38
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
Code
import math
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 ^^


img819.imageshack.us/img819/5121/hbhsig.png

Edited by ghxst on 20-07-10 13:52
Author

RE: Python Rounding problems

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
Newbie
Posted on 20-07-10 17:32
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.



img7.imageshack.us/img7/6588/1993.png
Author

RE: Python Rounding problems

ghxst
Member



Posts: 25
Location:
Joined: 12.05.10
Rank:
Newbie
Posted on 20-07-10 18:45
nope sorry, when I type 9 / 5 into the shell, I get 1. When I type 9 // 5 I get 1 too :s, someone told me // is to extract a root? ah I really don't know, but i don't get a decimal number on both cases


img819.imageshack.us/img819/5121/hbhsig.png
Author

RE: Python Rounding problems

tkearn5000
Member



Posts: 32
Location: US
Joined: 13.04.09
Rank:
Newbie
Posted on 20-07-10 19:01
9/5 will only give you a decimal in Python 3.1 or later. You need to be using floats instead of ints. Type 9.0/5 or 9/5.0 and see what happens. And read about integer division.


img7.imageshack.us/img7/6588/1993.png
Author

RE: Python Rounding problems

ghxst
Member



Posts: 25
Location:
Joined: 12.05.10
Rank:
Newbie
Posted on 20-07-10 19:42
Thank you so much tkearn5000

also thanks COM, told me right, I just didn't know how to do it right. I used float a bit wrong :s

tkearn5000 also pointed me into the right direction with round() =)

finally I can finish it!




img819.imageshack.us/img819/5121/hbhsig.png
Author

RE: Python Rounding problems

stealth-
Member



Posts: 1003
Location: Eh?
Joined: 10.04.09
Rank:
Mad User
Posted on 20-07-10 22:46
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 >.>


The irony of man's condition is that the deepest need is to be free of the anxiety of death and annihilation; but it is life itself which awakens it, and so we must shrink from being fully alive.
http://www.stealt. . .
http://www.stealth-x.com
Author

RE: Python Rounding problems

techb
Member



Posts: 384
Location:
Joined: 15.02.09
Rank:
Moderate
Posted on 20-07-10 23:10
also:
Code

>>>542/32
16
>>>
>>>542.0/32.0
16.9375




So:
Code

float(542/32)




should be:
Code

float(542)/float(32)





Also its a good idea to include a rounding digit; not needed, but good practice.
example:
Code

>>> round(542.0/32.0,0)
17.0
>>>




from there, you can convert to int or whatever.


www.userbars.com/74460/665255/337-7865-ubda3219.gif
kbcarte.wordpress.com
Author

RE: Python Rounding problems


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 21-07-10 01:26
You'd have less trouble, regardless of how new you are to a language, if you read the fucking replies.