Re: [Tutor] Dividing 1 by another number ?

2005-01-31 Thread Alan Gauld
> What Alan meant, presumably, was this:
>
> one = 1
> other = 42
> result = float(one)/other

Whoops! Yes indeedy!

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Kent Johnson
Brandon wrote:
PI = 3.14
Incidentally the math module has a more accurate value for pi:
 >>> import math
 >>> math.pi
3.1415926535897931
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Tony Meyer
> In that case you need it to use floating point numbers.
> The easiest way is to use 1.0 but if it comes from a table 
> or user entry you might have to explicitly convert:
> 
> one = 1
> other = 42
> result = float(one/other)

What Alan meant, presumably, was this:

one = 1
other = 42
result = float(one)/other

Otherwise the code simply gives 0.0 rather than 0, when we want ~0.0238.
Note that it doesn't matter which one you convert to a float, as long as one
of them is.

=Tony.Meyer

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Alan Gauld
> The archives of the list are available here:
> 
> 
> 

And on ActiveState's web site they are even searchable!

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Alan Gauld

> I'm trying to write a program they may involve needing to 
> divide 1 by another number. 

In that case you need it to use floating point numbers.
The easiest way is to use 1.0 but if it comes from a table 
or user entry you might have to explicitly convert:

one = 1
other = 42
result = float(one/other)

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Tony Meyer
> btw is there a place I can read everyones questions, 
> like I posted, or does this work my emailing a few 
> knowledgable people?

The archives of the list are available here:



(The messages from the future at the top are obviously ones where people's
mailers were in poor shape - ignore those).

=Tony.Meyer

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Terry Carroll
On Sun, 30 Jan 2005, Brandon wrote:

> I'm trying to write a program they may involve needing to divide 1 by
> another number. In the program below when I use 4 for the diameter of
> the bore, and 1 for the diameter of the rod, and 60 for the PSI, the
> force should be 706.8 . However the program keeps giving me 0 for
> "rodarea". 

In addition to the real division issue that's already been pointed out 
(simplest fix is to divide by 4.0 instead of 4 to avoid this), you've got 
another problem:

> area = (bore**2/4)*PI
> rodarea = (rod**2/4)*PI

Both of your formulas above are the same.  So, where bore = rod, as in 
your example where they're both equal to 1, area and rodarea are both 
going to evaluate to the same value:

>>> bore = 1
>>> rod = 1
>>> PI = 3.14
>>> area = (bore**2/4.0)*PI
>>> rodarea = (rod**2/4.0)*PI
>>> area
0.78503
>>> rodarea
0.78503

Now, look at your nest line:

> force = (area-rodarea)*psi

since area is equal to rodarea when bore is equal to rod, area-rodarea 
will always = 0, and force will equal 0, too, not 706.8 as you're 
expecting.

This isn't just a matter of when you use 1, it's true whenever you use 
equal values for rod and bore.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Sean Perry
Tony Meyer wrote:
Dividing two integers will give you an integer (truncated) result:
If you want '1/2' to give you 0.5 (throughout your script), you can do:
from __future__ import division
Notice that '//' (with or without the from __future__ import) will give you
the integer result.
or more simply, divide by 4.0 for rod and bore: (rod**2/4.0)
what happens if rodarea is bigger than area?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


RE: [Tutor] Dividing 1 by another number ?

2005-01-30 Thread Tony Meyer
> I'm trying to write a program they may involve
> needing to divide 1 by another number. In the
> program below when I use 4 for the diameter of
> the bore, and 1 for the diameter of the rod,
> and 60 for the PSI, the  force should be 706.8 .
> However the program keeps giving me 0 for "rodarea".
> If I use a value other than 1 the program works
> fine. am I missing something, this the 3rd program
> I have wrote.
[...]
> PI = 3.14
> area = (bore**2/4)*PI
> rodarea = (rod**2/4)*PI

Dividing two integers will give you an integer (truncated) result:

>>> 1/2
0

Dividing an integer by a float (or vice-versa) will give you a float:

>>> 1/2.0
0.5
>>> 1/float(2)
0.5

If you want '1/2' to give you 0.5 (throughout your script), you can do:

>>> from __future__ import division
>>> 1/2
0.5
>>> 1//2
0

Notice that '//' (with or without the from __future__ import) will give you
the integer result.

=Tony.Meyer

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Dividing 1 by another number ?

2005-01-30 Thread Brandon



I'm trying to write a program they may involve 
needing to divide 1 by another number. In the program below when I use 4 for the 
diameter of the bore, and 1 for the diameter of the rod, and 60 for the PSI, 
the  force should be 706.8 .
However the program keeps giving me 0 for 
"rodarea". If I use a value other than 1 the program works fine. am I missing 
something, this the 3rd program I have wrote.
 
Thanks
 
 
 
 
 
#program for calculating cylinder 
pressure#questions for the user
 
bore = input("What is the diameter of the cylinders 
bore?")rod = input("what is the diameter of the cylinders rod?")psi = 
input("What is the operating pressure?")
 
# formulas needed for end-result
 
PI = 3.14area = (bore**2/4)*PIrodarea = 
(rod**2/4)*PI
 
force = (area-rodarea)*psi
 
#prints the force the cylinder would 
have
 
print "The force is", 
force
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor