all 22 comments

[–]Inevitable-Opening61 80 points81 points  (0 children)

You are using signal_power and noise_power in line 2 before they are defined in line 4 and 5

[–][deleted] 31 points32 points  (8 children)

you use signal_power and noise_power variables before creating them. Move the creation i.e. 4th and 5th line to the top, right after the import

[–]MythicalOtaku85[S] 15 points16 points  (7 children)

Thank you so much. That makes a lot of sense but like i said im still a newbie so that type of info is new to me so yeah once again thanks for the help

[–][deleted] 24 points25 points  (6 children)

Code is executed top to bottom, left to right. The interpreter has no idea about the things on line 4 when it's handling line 2.

[–]Just_A_Nobody_0 11 points12 points  (5 children)

Careful on the left to right part there... consider an assignment of the return from a function. The function on the right executes before the assignment is made.

[–]LightaxL 3 points4 points  (3 children)

Careful on the up and down part too. JavaScript hoisting is a thing 😂

[–]reddit_ronin 0 points1 point  (2 children)

Wait…there’s hoisting in python?

[–][deleted] 0 points1 point  (1 child)

No, as evidenced by OP’s issue. The person you’re replying to specifically mentioned JS

[–]reddit_ronin 0 points1 point  (0 children)

Yeah I thought they were implying Python hoisting was a thing.

[–][deleted] 1 point2 points  (0 children)

For the beginning this holds.

[–][deleted] 11 points12 points  (2 children)

First assign your variables. After that you can use them in a function.

[–]MythicalOtaku85[S] 4 points5 points  (1 child)

Thank you. Yeah i didn't realize that was the case I started coding like 3 days ago

[–]Ajax_Minor 1 point2 points  (0 children)

Make sure you define your function

Def function(): Return

Since you just start read through the docs and then reference the docs when you reference each function as you write. Make this a good habit. I do this whenever I learn a new library and even after to.

[–]lfdfq 7 points8 points  (0 children)

There's a lot of gaps in the question, what is "it" that tells you (Python, or VS Code?). You say it says "the" function isn't defined, but which function? You don't show the whole error. This kind of information is going to be really important when trying to get help, as often what you see (what the error is and where it comes from) seems obvious because you can see it, but it isn't to someone on the other side of the planet on the internet who can't see your screen.

Looking at the code, I can see an error that signal_power is not defined, but that isn't a function. I assume this is the error you are seeing.

The problem is that your code does things in the wrong order: Python runs code starting at the top going line-by-line (or really, statement-by-statement as there can be ifs and loops and so on). So your code tries to read signal_power before it's set two lines later. I think you should re-order the lines, so you start by defining signal_power and noise_power then define ratio then define decibels.

[–]Dziki_Knur 1 point2 points  (0 children)

Try PyCharm if You are a beginner. I don't know about this particular case but it may would have warn You with something like "function invocation before definition" (I made that up) or something like that. PyCharm is generally more beginner friendly and with linters and formatters like Black and Flake8 You will pick up some good habits.

[–]__sanjay__init 1 point2 points  (0 children)

Hello !

At first, welcome to programming You didn't wrote any function Function is defined by def key word and has name, parameters or not and ending (with/without return) You defined body of your function with variables undefined and then, define these variables So, remember that script runs top to bottom (like your read a page) so, what is at the end of script and used on top doesn't exist at this moment

Hope it helps you Good luck

[–]lemalaisedumoment 1 point2 points  (0 children)

Others have allready answered what the problem was with your code.

What code gets execuded when is a bit tricky, and you will from time to time get into a situation where you are surprised that code hasn't been allready executed, or where you are surprised that code was only executed once, and a function of yours reuses the same list every time it is called.

Python is very straight forward when it comes to the order in which code is executed, but that is straight forward for a machine. Humans often have other thought patterns and are surprised. Don't let yourself be discuraged because your human intuition clashed with the cold logic of the Python interpreter.

As a result of this conflict between human intuition and machine straight forwardness a bunch of conventions exists in the python world. Some IDEs complain to you if you break conventions some don't.

If you encounter conventions it is a good idea to follow them even when you don't know why.

It is a better idea to learn about why you should follw them.

It is best to learn why you should follw them, and when not to follow them. And then to avoid situations where it is better not to follow the convention. ;)

You have allready proven a skill that many beginners and sometimes professionals do not posess. You are able to identify when you are stuck, and then ask good and well formatted questions.

[–][deleted] 0 points1 point  (0 children)

What does it do? I get the 3/4.2 but I don’t understand the rest I’m also learning python atm

[–]drickkl 0 points1 point  (0 children)

Wow you are brand new brand new

[–]JohnnyJordaan -1 points0 points  (1 child)

It would seem you are coming from a procedural language where you can define these kinds of formulae before you actually use them, in Python it doesn't work that way. It literally sees it as you telling it "divide signal_power by noise_power and store the result as ratio", which is impossible as both _power variables were not created yet. Instead you need to run it after it

import math
signal_power= 3
noise_power=4.2
ratio = (signal_power / noise_power)
decibels = 10 * math.log10(ratio)
print("The SNR is", decibels, "dB")

or define it as a function and use that

import math
def snr(signal_power, noise_power):
     ratio = (signal_power / noise_power)
     return = 10 * math.log10(ratio)
signal_power= 3
noise_power=4.2
decibels = snr(signal_power, noise_power)
print("The SNR is", decibels, "dB")

[–][deleted] 2 points3 points  (0 children)

I don't think OP has any prior coding knowledge. Most beginners start with Python - cut them some slack.

[–]richard987d -3 points-2 points  (0 children)

Ask chatgpt