#πŸ”’ need help installing functions

174 messages Β· Page 1 of 1 (latest)

wise pike
#

so I'm following a tutorial on coursera where they type in
print(square(3))
print(sub(3, ))

when he does it, the stuff somehow works.

But when I do it, the thing says "A is not defined"

surreal ospreyBOT
#

@wise pike

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

sacred drift
#

Where are you writing your code?

#

Into this web interface somewhere?

#

Or do you have your own setup?

#

I can take you through the idea of functions and their constituent concepts, if you need.

#

@wise pike

wise pike
sacred drift
#

Okay, may I see the code you've written there, please?

#

!code

surreal ospreyBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

wise pike
#

from my_functions import sub
print(sub(3))

sacred drift
wise pike
#

def square(x):
return x * x
def sub(x):
return a - b

I'm sorry I'm still new to Python so this might not be what you're asking but I got it form the my_functions module.

sacred drift
#

Is that the entirety of the my_functions module?

wise pike
sacred drift
#

Okay, great.

#

I can see what's wrong with the my_functions module.

wise pike
#

ok great. Cause I thought I did it right and then the computer said "SIKE you suck" and here we are\

sacred drift
#

What I'm currently doing is contemplating what I need to be telling you such that you can come to your own realisation of the problem.

#

Okay, let's start with variable assignment. You may have covered this concept, already, but let's just make sure the idea has a good foundation in your brain.

#

Explain to me your understanding of what a variable, specifically, is, and how you can create and use them.

wise pike
#

ok so a variable is like a number or a word. It's part of a string that is determined to be printed.

It can operate in either an interger(a whole number without the decimal)
a float(a number with a decimal like 3.14
or a string(a sentence like "Johnny has 5 apples)

#

they only are outputted if I say Print(, if I don't say print then nothing comes out cause I didn't tell python "put this on paper" cause python is a numbers ssytem.

sacred drift
#

Okay, now I'll give my explanation.

#

A variable is a named reference to an object. It's like a signpost. It's not the object itself, but can be treated as if it were the object.

#

A variable pointing to a string is not the string itself.

wise pike
#

what's a signpost?

sacred drift
#

A variable pointing to an integer object is not the integer object itself.

#

A road sign.

wise pike
sacred drift
#

Directions to find the object.

#

Like an index card in an old library system.

wise pike
sacred drift
#

It's not the book itself, but it tells you where to find the book.

#

We use variables to remember objects for later use. For example.

#

!e py my_int = 123 print(my_int)

surreal ospreyBOT
sacred drift
#

What here are the variables?

wise pike
#

123?

sacred drift
#

That's an integer literal. An object.

#

Not a variable.

wise pike
#

is it my_int?

sacred drift
#

That's one.

wise pike
#

is the other one =?

sacred drift
#

That's an operator.

wise pike
#

print?

sacred drift
#

Yes. That name there is a variable.

#

It refers to the print function object.

wise pike
#

ok solid solid.

sacred drift
#

Those are the two variables in this code. my_int and print

#

In order to use the objects to which they refer, those names have to exist.

wise pike
#

so variables are words

sacred drift
#

Or you have to be referring to the object more directly as a literal.

#

Variables are named references to objects. Yes.

#

Now, look at your my_functions module again.

#

Where has a been created?

wise pike
#

wait I'm confused when you said object I thought those were numbers only, are objects only words?

sacred drift
#

Where has b been created?

#

Okay, I'll drop back to talking about objects.

#

Python is known as an object oriented programming language.

#

Objects are packages of data that float around in memory.

#

They are data plus functionality for interacting with that data.

#

Like computers and keyboards.

#

Different objects, different buttons you can push and knobs you can twiddle and talk to and listen to the result of those interactions.

wise pike
#

so they're both numbers and words so long as they're in the print( parenthesis?

sacred drift
#

Each object has a type to which is belongs. A class.

#

The class describes the data structure, what data and functionality can and does exist in objects of its type.

#

For example.

#

objects of type str, strings, have str as their type.

#

I have a string

#

I have another string

#

They're both objects of type str

robust hemlock
sacred drift
#

But they are different strings

#

Then there are objects of type int

#

1

#

2

#

123

#

I can have lists

#

[1, 2, 3]

wise pike
#

so they don't need to be in the print parentesis to be classified as objects?

sacred drift
#

This is an object of type list.

#

Correct.

#

When they're in the print parentheses, it would be more apt to describe them as arguments.

#

But as a literal or as a variable or as an expression to be evaluated, they're all still objects.

wise pike
#

wait as arguments? so then how do I know which ones are the objects if they're oohhh wait if I say Int, is that an object?

sacred drift
#

Almost everything you're interacting with in Python is in some way an object. Only the syntax itself does not count as objects.

wise pike
#

syntax? Which button is that on the keyboard?

sacred drift
#

Syntax describes the structure of the language as you write it.

wise pike
#

huh?

sacred drift
#

Talk I 123 understand what you can seven five?$

#

Does this make sense?

wise pike
#

no

sacred drift
#

It shouldn't, because it's syntactically incorrect.

wise pike
#

sorry I dunno what syntactically means?

sacred drift
#

Grammar?

wise pike
#

ah.

sacred drift
#

So when we have variables, they are references to these data structures we call objects.

#

and can be used as a proxy to those objects

#

a reminder of how to find them

#

!e py print(123) # print 123 immediately var = 'abc' # remember 'abc' as var print(var) # almost like writing print('abc')

surreal ospreyBOT
wise pike
sacred drift
#

Yes.

#

A stand in for every purpose other than reassignment of the variable, taking the signpost and pointing it at a different object.

#

!e py var = 123 print(var) var = 'abc' print(var)

surreal ospreyBOT
wise pike
#

ok alright so how does this help me with the sub and the square thing I'm trying to get installed on my idle?

sacred drift
#

In order to use these variables, these names, they first have to have been assigned to an object.

#

Have a look in your my_functions module.

#

What's missing?

wise pike
#

ok I'm looking and so far I am not sure.

sacred drift
#

You try to use a and b...

#

Is there an a and a b anywhere else?

#

Where are they created?

wise pike
#

so far no, why would there be an a or b anywhere else? wouldn't that be like throwing car engines around the city without the car so the engine is useless?

sacred drift
#

!e py print(a)

surreal ospreyBOT
# sacred drift !e ```py print(a)```

:x: Your 3.13 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     print(a)
004 |           ^
005 | NameError: name 'a' is not defined
sacred drift
#

!e py a = 123 print(a)

surreal ospreyBOT
sacred drift
#

If you haven't told Python what a and b refer to, how can you expect it to do anything with a and b in the function?

#

You've given the function an x parameter.

#

But you don't use it.

sacred drift
#

Okay, let's talk a little about functions.

wise pike
tired void
wise pike
#

the x I put on sub(x)

sacred drift
#

!e ```py
def my_function():
print('Hello, world.')

my_function()
my_function()
my_function()```

surreal ospreyBOT
sacred drift
#

Here, I have created a function. It has no parameters (the names you write in the def line parentheses) and its sole purpose is to print the text, Hello, world.

wise pike
sacred drift
#

There is a variable assignment of my_function to the function object I have created.

tired void
wise pike
wise pike
tired void
#

Alright.

sacred drift
#

I'd like to talk about functions. πŸ™‚

tired void
#

When you say

def sub(x):
    return a - b

... where do you expect a and b to come from? What exactly should the code do when you tell it to a - b, given that you haven't told it what a and b are?

wise pike
sacred drift
#

We see that it runs the print three times.

#

Functions can also accept things called arguments. Arguments are objects given in the call. I will demonstrate.

wise pike
sacred drift
#

!e ```py
def my_function(parameter):
print(parameter)

my_function('I am an argument.')```

surreal ospreyBOT
sacred drift
#

Here, we see that the string given in the call, 'I am an argument' is the argument. It has the name parameter assigned to it within the context of the execution of the function.

#
parameter = 'I am an argument.'```
#

As if I were doing this.

#

But inside the function.

#

We can also give functions multiple parameters.

#

!e ```py
def my_function(a, b):
print(a)
print(b)

my_function('apples', 'bananas')```

surreal ospreyBOT
sacred drift
#

Here, a and b are the parameters/variables.

#

'apples' and 'bananas' being the arguments.

#

If you need, you can even substitute variables for the literals.

#

!e ```py
def func(obj):
print(obj)

var = 123
func(var)```

surreal ospreyBOT
sacred drift
#

See how this works?

#

Parameters are like a function's inbox.

#

The stuff you feed the parameters are the arguments.

#

Functions can have a lot of different parameters, and different ways of describing how they are to be provided. This is just the simple, positional way.

wise pike
#

I'm sorry I know you mean well I'm just having a tough time comprehending this information. You've helped me understand some concepts but I'm not as advanced in knowledge as you are.

So a lot of this is going over my head.

Sorry I'm a bum I'm not as smart as most people on this server.

sacred drift
#

This feeling is normal.

#

Don't worry about it. πŸ™‚

#

Everyone feels it when they're starting off.

wise pike
#

thanks for understanding, I just picked up python a few months ago and it's not a straight shot like i was hoping, there's so many twists and turns that I think I fell on my head several times

sacred drift
#

Shall I continue, would you like to take a break, or would you like to terminate the session?

sacred drift
#

Very well.

#

@ me when you're prepared to proceed.

surreal ospreyBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.