gpujol
gpujol

Reputation: 63

error python: a float is required

I am new here, and I'm getting used to programming with Python. I have been searching through the web looking for useful answers but, it was impossible to find a solution to my problem.

Here it goes:

radiation=1.3888 
n=17
LAT=51.05

def dec(n):
    if 0<n<365:
        dec=23.45*math.sin(math.radians(360*(284+n)/365))
        print(dec)
    else:
        print('the day',n,'is not valid')

def wss(LAT,dec):
    wss=math.degrees(math.acos(((math.tan(math.radians(LAT)))*math.tan(math.radians(dec)))))
    print(wss)

--- When I run this code this is what I receive:

>>> dec(n)
-20.91696257447642

>>> wss(LAT,dec)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:/Users/Gerard/Dropbox/Master Thesis Gerard Pujol/Master Thesis Work/work hourly radiation OK.py", line 25, in wss
    wss=math.degrees(math.acos(-((math.tan(math.radians(LAT)))*math.tan(math.radians(dec)))))
TypeError: a float is required

I don't know why the Python gives me this type of error: 'a float is required'.

I have tried a lot of modifications but have been useless. I hope somebody has a solution to my problem. Thank you very much!

Upvotes: 1

Views: 7284

Answers (5)

Saket Jain
Saket Jain

Reputation: 1382

    radiation=1.3888

    n=17

    LAT=51.05

    def dec(n):

        if 0<n<365:

            dec=23.45*math.sin(math.radians(360*(284+n)/365))
            print (dec)
            return dec

        else:

            print('the day',n,'is not valid')
            return -1


    def wss(LAT,dec):

       wss=math.degrees(math.acos(((math.tan(math.radians(LAT)))*math.tan(math.radians(dec)))))

        print(wss)

Then do:

>>> dec = dec(n)
-20.91696257447642
>>> wss(LAT,dec)

This will work. To know why your code didn't work, read on.

What you are doing with def wss(LAT,dec) is that you are defining a function that passes two arguments LAT and dec. However, when you are actually calling that function (e.g. >>>wss(LAT,dec)) you have not set any value for dec. You have a value in dec(n) but you have not assigned it to anything. Contrast this with the variable LAT to which you assigned a value. (Remember LAT=51.05?)

Upvotes: 1

bruno desthuilliers
bruno desthuilliers

Reputation: 77942

You first have to actually return something from the dec() function - neither assigning to a local name nor printing to stdout won't do:

def dec(n):
    if not  0 < n < 365:
        # that's how you handle incorrect arguments in Python
        raise ValueError("'%s' is not a valid day number" % n) 

    # 'n' is valid, let's proceed: 
    return 23.45 * math.sin(math.radians(360 * (284 + n) / 365.0))

Now you can use this value either thru an intermediate variable:

LAT = 51.5
n = dec(17)
print wss(LAT, n)

Or just skip the intermediate variable:

print wss(51.5, dec(17))

NB : if using Python 3.x, replace print <something> with print(<something>)

Upvotes: 0

MBR
MBR

Reputation: 824

What you want to do is something like

def dec(n):
    if 0<n<365:
        dec = 23.45*math.sin(math.radians(360*(284+n)/365))
    else:
        print('the day %d is not valid' %n)
        return
    return(dec)

and then call

wss(LAT, dec(n))

dec here is a function, so you cannot pass it as an argument. Since dec is returning a float, I guess that's what you actually want to retrieve.

Upvotes: 0

Tomek Urbańczyk
Tomek Urbańczyk

Reputation: 187

First of all, your formatting is terrible. Learn how the code should look like - it will save you a lot of time later on, especially while debugging.

Secondly,when you run: wss(LAT,dec)

You pass 2 arguments to function 'wss', first one is LAT = 51.05, but you have never defined an argument called 'dec' - you have defined such a function.

What you want to do is something like this:

import math

n=17
LAT=51.05

def CalcDec(n):
    if 0<n<365:
        dec=23.45*math.sin(math.radians(360*(284+n)/365))
        return dec

def CalcWss(LAT,dec):
   wss=math.degrees(math.acos(((math.tan(math.radians(LAT)))*math.tan(math.radians(dec)))))
   return wss

print CalcWss(LAT, CalcDec(n))

Remember to take care of possible exceptions. And read a bit about good programming practices...

Upvotes: 0

Paul Evans
Paul Evans

Reputation: 27577

The variable dec is used both for a function name and a variable. Python is seeing it as a function name in the error.

Upvotes: 0

Related Questions