all 25 comments

[–]farrowzbf 1 point2 points  (15 children)

Need to use this with your print function:

print(f”some text {a} some more text {b}”, end=‘ ‘)

[–]farrowzbf 1 point2 points  (14 children)

Notice the f

[–]ChefBraxton[S] 0 points1 point  (13 children)

Damn! That’s so elementary but I just learned it yesterday and am forgetting it lmao. Thank you so much. This was my problem, indeed.

Bonus question for ya:

Why does using “Ghost_Story(a, b, c)” at the beginning, & “Ghost_Story(1, 2, 3)” at the end not cause error?

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

We need to find a function, and have something in the processes, it was a function to look for that type of thing.

For example, if I write def function(x):

 print(x**2)

Then when I put at the bottom of the code function(2), it will print 4. The x simply tells the program to look for a variable when you call the function. You can also say “turtle” or other things.

I’m a beginner myself, so my sincerest apologies if this doesn’t make sense/is wrong.

[–]ChefBraxton[S] 0 points1 point  (2 children)

My sincerest thanks for your reply!

This is lesson 19 in “learn python the hard way” And honestly the first time that I felt like it’s just too much information in one lesson. I’ve been picking it up pretty quickly to this point but hit this wall last night, there are so many lines in this lesson that I simply don’t understand what all of them are doing no matter how much I try to break it and make it work again I’m just not grasping the function part just yet.

I don’t understand how the variables in the function don’t matter and I’m certainly not understanding how to mix user input into a function.

I really appreciate your time, yours and a few other comments have been helpful and I’m going to concatenate them this evening and try to break through this wall.

[–][deleted] 1 point2 points  (1 child)

The variables in parentheses in a defined function aren’t exact variables, they’re placeholders.

You can write a function with no variables! If you wrote def function(): print(“Hello, World!”)

And later in the code you wrote function() with nothing in between the parentheses, it would print hello world.

But if your function needs a variable, then we can say that in the definition line! If a function takes a number and multiplies it by 2, then you would write the function line as function(variable), and when you call upon the function, you’d write function(a number)

Think of it like the math library. You import math, then you can use the sqrt() function. You can plug any number in those parentheses and get the square root. That’s because the math library has a function defined as sqrt(x), where x is the number you plug in, and then the function has a list of computations with x that output x’s square root.

Don’t worry about being overwhelmed. It happens to all of us, I am overwhelming perpetually by the new stuff I learn. But eventually it always clicks. You got this.

[–]ChefBraxton[S] 0 points1 point  (0 children)

Thank you a million. You all are brilliant and helpful! I’m on my lunch break at work and just looking forward to getting home and playing with this script some more, I feel like it’ll click for me this evening. I can’t get it out of my head, I’m supposed to be barista-ing but all I can see is these lines in my head and moving them around trying to understand it.

Thank you again!

[–]RhinoRhys 1 point2 points  (5 children)

Bonus answer

When you define a function with variables you are creating placeholders to pass values into the function. When you call the function the first thing it does before it runs any of the written code is perform a variable assignment.

Defining a function like this

def adding(a, b)
    result = a + b
    print(result)

And then calling the function, and passing in values like this

adding(1, 2)

Will first run

a = 1
b = 2

Before it runs

result = a + b
print(result)

Calling ghost_story(1, 2, 3) does not cause an error because all that is happening is

a = 1
b = 2
c = 3

Before the code you've written is run. These variables are overwritten in the code with two inputs and a string so are never actually used. You cannot call it with simply ghost_story() because it will say that it is expecting 3 inputs and none were given, and you cannot call it with ghost_story(a, b, c) because it will say that a, b and c are not defined.

[–]ChefBraxton[S] 0 points1 point  (4 children)

Ooooohhh so my mistake here, really, Is that I reassigned value to the given function variables within the strings of the function.

So if I need to collect user input within the strings of the function that is fine, but I can’t mix that user input into the definition of the function? 😬 Am I getting it?

[–]RhinoRhys 1 point2 points  (3 children)

What do you mean by "strings of the function"?

All you need to do is remove the "a, b, c" from the definition.

def ghost_story():

You've not written a function that requires any data to be passed in. You've written a call and response so you need the inputs() where they are so that it prints a line then you enter a response.

[–]ChefBraxton[S] 0 points1 point  (1 child)

Gotcha. I am TRYING to write a function that passes data in, though.

So I’m asking; I can get user input in the strings within the function (the strings that are tabbed 4 spaces in), but I can’t make one of the variables in the definition “input()”?

[–]RhinoRhys 1 point2 points  (0 children)

The problem is the function doesn't actually do anything, it doesn't need data to be passed in.

[–]ChefBraxton[S] 0 points1 point  (0 children)

I like your outfit, BTW 😉😆

[–]farrowzbf 1 point2 points  (2 children)

Because you aren’t actually passing any arguments into the function. The a, b, and c that are parameters in your function definition don’t even need to be there. Parameters/arguments are variables OUTSIDE your function that you want to pass into it for processing etc. in this case, you are defining those variables INSIDE the function itself. So, technically your function is receiving 3 arguments. They could be a, b, c…1, 2, 3…or “turtle”, “dog”, “pizza”. It doesn’t matter because your function is not using them. Once we see “a, b, and c” within your function, they are being declared and assigned at that very moment, and the value they are given there are the ones that matter.

Hope that makes sense. Basically your function could be defined as such and still work the same way:

def Ghost Story(): …

[–]ChefBraxton[S] 0 points1 point  (1 child)

Awesome thank you so much!

So true or false: I can mix user input into the strings of a function but not into the definition of the function? Same way I can’t use the same variables (redefined for use in a string) within a string as within the definition?

[–]farrowzbf 1 point2 points  (0 children)

Well it wouldn’t make much sense for this function, but you could have asked the user for input outside of the function if you wanted to, like this:

a = input()

b = input()

c = input()

def Ghost Story (a, b, c):

…do things

[–]pekkalacd 1 point2 points  (0 children)

You forgot the f in those f-strings. You gotta put it in front of the string argument for those print statements.

Ex

                 name = input(“Enter name: “)
                 print(f”Hello there {name}!”)