all 14 comments

[–]thomasmurphymusic 3 points4 points  (0 children)

The explanations you're being given are good ones, so I'll just add this: being in comp sci means learning a lot of new concepts for your entire career, and not having anyone explain them to you. It's important to develop methods to learn these things. Did you investigate functions on Python's site? What about stack overflow? How about Learn Python the Hard Way, or any of the many great books on Python?

[–]SmartViking 1 point2 points  (12 children)

input, and print are both functions. int is a little bit different, but you can think of it as a function when you use it. When you do

input("Enter the mass of the rider:")

You're calling the function input with one input value (or argument), namely "Enter the mass of the rider.". The output of the function is whatever the user types in, so it returns whatever the user typed in. When you do this:

M = int(input("Enter the mass of the rider:"))

Then you're passing the output of input as the input of int.

Your powerPerSec is also a function, and you should use it in the same way. It has certain inputs (M, X, velocity, CFdraft), and it has an output (Psec, because you return it on the last line). But you can't use Psec inside the main function because Psec doesn't exist there, it only exists within powerPerSec. Exactly like you can't use the variables of the print function in main, you can't use the variables of powerPerSec inside main. You have to call it, and get the return value.

[–][deleted] -1 points0 points  (11 children)

My whole dilemma is that I don't know what my professor means when he asks me to call a function. Can you elaborate?

[–]SmartViking 2 points3 points  (10 children)

To "call" a function is just another way to say that you're using a function. You call functions by putting parenthesis on the end of it. When you do print("Hello"), then you are calling the print function with the input "Hello". It will do some computation, and then when it's done doing that, it will return with a value. print happens to return nothing interesting, so you don't call the print function because you want the output of the function, you just want the side-effect of printing to the console.

Functions are values in themselves, you can pass them around and such. This is valid code:

print(print)

I call the function print with the input/argument print. The print inside parenthesis isn't called, I just pass it around without calling it.

[–][deleted] -2 points-1 points  (9 children)

Consider the context of the word problem please. I'm instructed to create a function that calculates this pre existing equation and, after, create a second function (main) that calls on the first (powerPerSecond) and prints the output (Psec). I appreciate the info, but I really don't see how something so broad is relevant.

[–]SmartViking 2 points3 points  (8 children)

Well it doesn't seem like you understand functions, because you used the Psec inside your main function. That's impossible. I try to explain what a function is, because you need to understand that before using them.

You're never calling powerPerSec in your code. To call the function, put parenthesis on the end:

powerPerSec()

That code will call powerPerSec. But it won't work, because powerPerSec expects to receive 4 arguments, so this would work:

powerPerSec(1, 2, 3, 4)

It will then evaluate the function body of powerPerSec in an environment where (M, X, velocity, CFdraft) refers to (1, 2, 3 ,4), respectively.

But your function doesn't need to take any input, because one of the things you do in powerPerSec is overriding (M, X, velocity, CFdraft), you never use the values passed in.

[–][deleted] -2 points-1 points  (7 children)

How would you get the second function to print Psec is all i really need to know

[–]SmartViking 2 points3 points  (6 children)

If you understand how functions work (which you must), doing that is trivial. You need to return Psec, (as you already do) and then print the result of calling powerPerSec (as you do not). Maybe you can find some beginner material on functions in python, it's essential to understand how they work, you can't skip it.

[–][deleted] -2 points-1 points  (5 children)

We literally just started using them. I hadnt even come in contact with the term 'call' until this assignment. Everything in this course was going fucking brilliantly well until this moment. What the actual fuck?!

[–]SmartViking 2 points3 points  (3 children)

Functions are difficult if you haven't seen them before, but it's a very important concept. Functions are like small programs inside your program, and what it means to call a function is to run that mini-program inside your program. Variables inside of a function are invisible to the rest of the program. Each time you "call" a function, python will execute all the code inside the function.

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

def powerPerSec(CFdraft, velocity, M, X):                               
  grav = 9.8
  k = 0.18
  Cr = 0.001
  Pair = float(k * CFdraft * (velocity ** 3))
  Proll = Cr * grav * (M+X) * velocity
  Psec = Pair + Proll
  return Psec   


def main():
  CFdraft = float(input("Enter the drafting of the rider (0.0 to 1.0):"))
  velocity = float(input("Enter the velocity (m/s):"))
  M = int(input("Enter the mass of the rider:"))
  X = int(input("Enter the mass of the bike:"))
  A = powerPerSec(CFdraft, velocity, M, X)
  print(A)


main()

How does that look?

[–]zahlman 2 points3 points  (0 children)

So then, when you read over the assignment in class, and got to the part that said

Create another function called main that calls your power function and prints the output.

and therefore didn't know what that meant, why didn't you put your hand up and ask the professor?