#๐Ÿ”’ Define function

77 messages ยท Page 1 of 1 (latest)

arctic arrow
#

How does this work?

cinder gateBOT
#

@arctic arrow

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.

arctic arrow
#
def numbers(num="Please input an number :) "):
    print(num)
    x = int(input("Enter a digit: "))
    y = int(input("Enter another digit: "))
    print(divide(x))
    print(divide(y))
    

def divide(n):
    return n // n

numbers()

I'm only receiving the output 1.0

rapid frost
arctic arrow
#

oh

#

so I'm suppose to change n to x and y I suppose?

brave oar
#

You're only passing one argument to the function.

arctic arrow
#

I need another argument?

brave oar
#

If you want to use another variable, yes.

#

Is this homework?

arctic arrow
#

No

#

I'm just learning how to code

brave oar
#

!res

cinder gateBOT
#
Resources

The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.

brave oar
#

There are some great resources there.

arctic arrow
#

Very much appreciated

#

So I need to pass another argument to divide then?

brave oar
#

Well right now you are only passing one value at a time to the function. So since your function executes return n // n then divide(3) will return the result of 3 // 3

#

and so on for any number you call it with

#

If you want to specify different values for the numerator and denominator you need to pass the function two separate values. The way you tell Python that you want to do this is by defining the function to take two arguments.

arctic arrow
#

I see

brave oar
#

!e ```py
def divide(x: int, y: int) -> int:
return x // y

print(divide(10, 5))

cinder gateBOT
#

@brave oar :white_check_mark: Your 3.12 eval job has completed with return code 0.

2
brave oar
#

Do you understand?

#

it's replacing x with 10 and y with 5

arctic arrow
#

ahh I see

#

so if I change it to

def divide(x: int, y: int) -> int:
  return x // y

print(divide(6, 2))

It would change x and y to 6 and 2 then

brave oar
#

you got it

arctic arrow
#

So I have to call divide instead?

brave oar
#

Instead of what?

arctic arrow
#

numbers

brave oar
#

No, your numbers function is doing something different.

arctic arrow
#

TypeError: divide() missing 1 required positional argument: 'y'
It gives me this error

#

I don't understand why

brave oar
#

show your code

arctic arrow
#
def numbers(num="Please input an number :) "):
    print(num)
    x = int(input("Enter a digit: "))
    y = int(input("Enter another digit: "))
    print("x divided by y is", divide(x / y))
    

def divide(x:int, y:int):
    return x // y


numbers()
brave oar
#

Why are you using / there? You separate arguments when calling a function with ,

#

The reason you're getting that error is that Python is evaluating x / y and passing that value as a single argument.

arctic arrow
#

ohhh

#

I didn't know that it counts as an argument

wild lake
#
def numbers(num="Please input an number :) "):
    print(num)
    x = int(input("Enter a digit: "))
    y = int(input("Enter another digit: "))
    print("x divided by y is", divide(x,y))
    print(f"x divided by y is {divide(x,y)}")
    

def divide(x:int, y:int):
    return x // y

numbers()```
brave oar
#

@arctic arrow How are you learning Python?

arctic arrow
#
def numbers(num="Please input an number :) "):
    print(num)
    x = int(input("Enter a digit: "))
    y = int(input("Enter another digit: "))
    print("x divided by y is", divide(x,y))
    

def divide(x:int, y:int):
    return x // y


numbers()

It does work now

brave oar
#

fantastic

arctic arrow
#

python to be exact

brave oar
#

Ok, that's cool. Also check that site I mentioned earlier because those resources can help you as well.

arctic arrow
#

I see I see

#

Give me a moment, I still don't understand something on this line

def numbers(num="Please input an number :) "):
    print(num)
    x = int(input("Enter a digit: "))
    y = int(input("Enter another digit: "))
    print("x divided by y is", divide(x,y))
    

def divide(x:int, y:int):
    return x // y


numbers()

arctic arrow
arctic arrow
#

This is where I'm confused about

#

how they are related

brave oar
#

In the image you just posted you are calling square(x) on line 3.

#

You are defining the function on line 6

#

You are assigning the value of calling input("What's x? ")) to x on line 2

#

and on line 10 you are calling main, which performs those other operations.

arctic arrow
#

I see

#

But in the picture, line 6 is not x?

brave oar
#

the names of arguments do not matter

#

they are placeholders

#

Have you taken algebra?

arctic arrow
#

yeah

brave oar
#

like x or y or z in algebra

arctic arrow
#

yeah

brave oar
#

since you square a number by multiplying it by itself it makes sense that square only takes one value.

arctic arrow
#

yeah

brave oar
#

Do you understand now?

arctic arrow
#

So essentially, the

def numbers(num="Please input an number :) "):
    print(num)
    x = int(input("Enter a digit: "))
    y = int(input("Enter another digit: "))
    print("x divided by y is", divide(x,y))
    

def divide(x:int, y:int):
    return x // y


numbers()

the print("x is divided by y is", divide(x, y)), the x and y's are connected to the assigned variables on top,
the def divide() however is a function itself that runs itself with two other variables regardless of def numbers() doing of x and y, acting as a clone?

#

Not sure if my explanation makes sense or not

#

but how I would see def divide() and the arguments inside it is just a way to act as a clone for the assigned values x and y to function?

wild lake
#

This is the 12th video in my python programming series. Today I talk about functions and their uses.

Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/beginner-python-tutorials/functions/

Please LIKE and SUBSCRIBE for more content!

Video Tags:
python,python tutorial,python language,python full course,python course,lear...

โ–ถ Play video
cinder gateBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.