-1

I have this code and I get an error when I tried to convert to farenheit. It's basically a windchill calculator:

    import math
c = ""
f = ""
t = 0


def temp (t):
   t = (9/5 * temp_chosen) + 32
temp_chosen = float(input("What is the temperature? :"))
scale = input("Farenheit or Celsius (F/C)? ").upper()   

def wind():
   if scale == "C":
      return (t)
      print(t)

   else:
      t = temp_chosen
      print(t)

      for i in range (5, 65, 5):
         wind_chill = 35.74 + (0.6215 * t) -35.75 * (i ** 0.16) + 0.4275 * ((t)) * (i ** 0.16)
         print(f"At temperature {t}F, and wind speed {i} mph, the windchill is: {wind_chill:.2f}F")


temp (t)  
wind ()

And I get this error:

Traceback (most recent call last):
  File "c:/Users/Azevedo/Documents/Codes/test.py", line 28, in <module>
    wind ()
  File "c:/Users/Azevedo/Documents/Codes/test.py", line 14, in wind
    return (t)
UnboundLocalError: local variable 't' referenced before assignment

How can I fix that?

  • Please fix the indentation of your code. It's impossible to know what you meant to write. – Brian61354270 Apr 08 '21 at 01:25
  • 4
    Even aside from the indentation, I'm sorry to say, but most of this code shows a fundamental lack of understanding in how variables and functions work. I recommend a good beginner Python tutorial. Take it slow and learn one concept at a time. Throwing together a bunch of code you don't understand accomplishes nothing. – Silvio Mayolo Apr 08 '21 at 01:26
  • basic indentation errors in your code. As @SilvioMayolo said take a good beginner python tutorial and take one step at a time to understand it. – Hassaan Ali Apr 08 '21 at 01:28

3 Answers3

1

You are trying to return a value that is not defined. Well it is defined but you need to pass it in to wind. Try this piece of code, fixed a couple bugs you had.

import math
c = ""
f = ""
t = 0


def temp (t):
   t = (9/5 * temp_chosen) + 32
   return t
temp_chosen = float(input("What is the temperature? :"))
scale = input("Farenheit or Celsius (F/C)? ").upper()   
def wind(t):
   if scale == "C":
      return (t)

   else:
      t = temp_chosen
      print(t)

      for i in range (5, 65, 5):
         wind_chill = 35.74 + (0.6215 * t) -35.75 * (i ** 0.16) + 0.4275 * ((t)) * (i ** 0.16)
         print(f"At temperature {t}F, and wind speed {i} mph, the windchill is: {wind_chill:.2f}F")


t = temp (t)  
wind (t)
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
1

You need to add global t at the top of your function. Because, Python works in scopes : In your case one module or script scope that holds all variables, functions, etc. that are directly in the script (e.g. not indented at all).t is in this one. And a function scope for each function, each holding the variables of the function. In that one you try to reference t. Scopes are not nested, instead inddependent of each other. By adding global t you make Python look for t in the module scope instead of in the function scope. Note please, that global variables are considered bad design practice, you can find out why here. Besides of that, you have to move the return below the print, else it wont get executed. return immediately returns, all code after it will never be executed.

TheEagle
  • 5,808
  • 3
  • 11
  • 39
1

I just cleaned your code a little bit. Also, I fixed the indentation.

import math
c,f = "",""
t = 0

def wind():
    temp_chosen = float(input("What is the temperature? :"))
    scale = input("Farenheit or Celsius (F/C)? ").upper()  
    t = (9/5 * temp_chosen) + 32
    if scale == "C":
        print(t)
    else:
        t = temp_chosen
        for i in range (5, 65, 5):
            wind_chill = 35.74 + (0.6215 * t) -35.75 * (i ** 0.16) + 0.4275 * ((t)) * (i ** 0.16)
            print(f"At temperature {t}F, and wind speed {i} mph, the windchill is: {wind_chill:.2f}F")

if __name__ == "__main__":
    wind()
Hani
  • 154
  • 6