PDA

View Full Version : odd python problem?

ricky_cullen
May 2nd, 2008, 10:46 PM
i have written a python script to help me speed up how long it takes me to do my math homework, however it refuses to work right.
Here is the code

import math
print "to convert from radians to degrees\
use p as pi"
p=math.pi
print y

i get the output as

file "test.py", line 6, in <module>
TypeError: a float is required

Do note i look at the sticky about homework and i'm not trying to braek any rules i've converting degrees into radians for a while now i just want to speed it up a little and push my programming up a little bit.

aamukahvi
May 2nd, 2008, 10:54 PM
Hi,

as it says a float is required so you cannot provide a string.

#!/usr/bin/env python

import math
from decimal import Decimal

print "to convert from radians to degrees\
use p as pi"
p=math.pi
print y

ricky_cullen
May 2nd, 2008, 10:57 PM
ok i see, one of the many things i have to take note of in the world of programming lol. thanks

LaRoza
May 2nd, 2008, 10:58 PM
i have written a python script to help me speed up how long it takes me to do my math homework, however it refuses to work right.
Here is the code

import math
print "to convert from radians to degrees\
use p as pi"
p=math.pi
print y

i get the output as

file "test.py", line 6, in <module>
TypeError: a float is required

raw_input returns a string. Use float() to convert to a decimal, and int() to convert to an integer.

import math
print "to convert from radians to degrees\
use p as pi"
p=math.pi

aamukahvi
May 2nd, 2008, 11:03 PM
float(x)
Neat! I was sure there had to be an easier way :D

ricky_cullen
May 2nd, 2008, 11:07 PM
import math
print "to convert from radians to degrees\
use p as pi"
p=math.pi
try:
except(ValueError):
print "That is not a number"

this is the only one that has worked but it spits back that is not a number when i puy in p/2 which is i worked it out to test the program before hand and it is 90 degrees

i responed to aamukahvi responce before i got to try it out, it gave me another error but removed the one i that was stopin me :D which is what i asked for lol

LaRoza
May 2nd, 2008, 11:09 PM
this is the only one that has worked but it spits back that is not a number when i puy in p/2 which is i worked it out to test the program before hand and it is 90 degrees

i responed to aamukahvi responce before i got to try it out, it gave me another error but removed the one i that was stopin me :D which is what i asked for lol
I don't understand what you are saying. You entered "p/2"? I don't think you want to do that. If you want to express it that way, just enter the number, not "p".

Here is how I would have written it, from your description:

import math
print "To convert from radians to degrees."
p=math.pi
if x.isdigit():
else:
print "That is not a number"

ricky_cullen
May 2nd, 2008, 11:14 PM
it gives me the same output but i think i might try to use the unit circle my teacher gave me so it will print out the exact value from it instead of working out the numbers itself becuase the more i look at it i reliaze it might be hard to look at if it is a really long Decimal so it might be easier and better for my purpose. but thanks to both of you you helped me over my frist bump in programming :)

Can+~
May 3rd, 2008, 01:36 AM
#!/usr/bin/env python

import sys, math

for i in sys.argv[1:]:
try:
except ValueError:
print "Not a float"

def start():
if not len(sys.argv[1:]):
print "Usage:",sys.argv[0],"float1 float2 float3... floatn"
else:

if __name__ == "__main__":
start()

Normal usage:

python radians.py 15 20 17 30

In case of non-float values:

python radians.py 15 NOT A FLOAT 30

In case of no input:

Usage: python radians.py float1 float2 float3... floatn

Usage: ./radians.py float1 float2 float3... floatn

A bit excessive example. Although, if someone has a better solution than the if not len(sys.argv[1:]) . Since I don't like using LBYL style.

ricky_cullen
May 3rd, 2008, 02:08 AM
wow... i would have never had come up with anything like that :o

Quikee
May 3rd, 2008, 01:17 PM
You could also build yourself a custom python based calculator ;)

import math
print "Type 'exit' to exit the program!"
while 1:
string = raw_input("Calculate: ")
if string == "exit":
break

And use it like:

Type 'exit' to exit the program!
Calculate: deg(pi/2)
90.0
Calculate: deg(pi/2) * 2
180.0
Calculate: pi/2
1.57079632679