My code is not working as I imagined it to... (beginner coder here)

My code isnt outputting the expected output

The code consists of a module file (degree.py) and 6 other files that run the module and output a temperature in (C, F or K) from user input in (C, F, K) in other words, the 6 files converts between the different degrees respectively Heres the module file (degree.py):

#degrees (Celsius, Fahrenheit, Kelvin)

bruh = float(0)
c_1 = float((5/9) * (float(bruh) - 32)) #F to C
c_2 = float((float(bruh) - 273.15) * 1) #K to C
f_1 = float(((9/5) * float(bruh)) + 32) #C to F
f_2 = float((float(bruh)-273.15) * (9/5) + 32) #K to F
k_1 = float((float(bruh) + 273.15) * 1) #C to K
k_2 = float((float(bruh)-32) * (5/9) + 273.15) #F to K

And heres one of the 6 files (fahrenheitToCelsius.py):

import degree

test = False 
while test == False:
    bruh = input("Enter a positive numerical value: ")
    bruh.isnumeric() 
    if bruh.isnumeric() == True:
        print("If the temperature is", bruh, "degrees in Fahrenheit, then it is", degree.c_1, "degrees in Celsius.")
        print()
        print("Do acknowledge that the calculations done here in Python alone are not going to be precise.")
        test = True
    else:
        print("This was not a numerical value bruh :(")
        test = False

As the name suggests, this specific code was supposed to convert a value from degree F to C.

However I am noticing that the outputs are incorrect, first I thought it was a float margin error, but I soon to find out the culprit could be from the value bruh somehow becoming 0

I would really like to be enlightened into why the code isn’t working. I would really appreciate it if someone could help me solve this problem.

If those are in separate files, they have separate namespaces. That means the name bruh in one file is actually completely separate from the name bruh in another.

This seems like a really good place to learn about functions. Try turning all of those calculations into a series of functions:

def c_to_f(bruh):
    ...

def c_to_k(bruh):
    ...

def f_to_k(bruh):
    ...

Then you can call those functions as degrees.c_to_k(bruh) or whichever one is appropriate.

(You MAY also want to consider calling the variable degrees or something, but that’s completely up to you :slight_smile: )

1 Like

Welcome, and congratulations on formatting your code correctly in your first post. You should get some kind of award :joy:

Per your question, what @Rosuav said: this is a great time to learn about functions.

As an aside: all of the code in the module namespace of degree.py is being executed when you import it at the top of fahrenheitToCelsius.py.

So what happened in your first attempt is that you executed degree.py, which take the value bruh = 0.0 and creates c_1, the value in ÂşC if bruh was ÂşF. Regardless of what you input later, that will be the value of degree.c_1 in your other script.

To actually pass the value you input into your code, you need to write a function as @Rosuav suggests.

1 Like

What exactly was unclear about this when you asked on Stack Overflow?

There are two problems.

First, every module has its own set of variables. If you want to use one module’s variables from another, it is not enough to just import; that only makes the module itself available - if you want to use the things that it defined, you have to “reach in” using the . notation.

Second, any time that you write a variable name, and then =, and then some other code, that means: figure out the value right now, and use the variable as a name for the result. It does not store a “formula” that will update later. Therefore, changing bruh later will not change anything else to change that was calculated based off of it. You need to do that calculation after getting a value from the user.

1 Like

Got it, I did learned more about this after yesterday and today I solved the issue, thanks for help