#๐ Define function
77 messages ยท Page 1 of 1 (latest)
@arctic arrow
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.
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
In the divide function, you are taking a number n, and dividing it by itself. Any number divided by itself is always 1
You're only passing one argument to the function.
I need another argument?
!res
The Resources page on our website contains a list of hand-selected learning resources that we regularly recommend to both beginners and experts.
There are some great resources there.
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.
I see
!e ```py
def divide(x: int, y: int) -> int:
return x // y
print(divide(10, 5))
@brave oar :white_check_mark: Your 3.12 eval job has completed with return code 0.
2
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
you got it
So I have to call divide instead?
Instead of what?
numbers
No, your numbers function is doing something different.
TypeError: divide() missing 1 required positional argument: 'y'
It gives me this error
I don't understand why
show your code
doesn't this function essentially have 2 arguments?
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()
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.
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()```
@arctic arrow How are you learning Python?
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
fantastic
Harvard CS50 course
python to be exact
Ok, that's cool. Also check that site I mentioned earlier because those resources can help you as well.
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()
Is line 3 essentially connected to line 2 or 9?
Cause this seems like line 5 is connected to line 8 just pretending theres lines
This is where I'm confused about
how they are related
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.
yeah
like x or y or z in algebra
yeah
since you square a number by multiplying it by itself it makes sense that square only takes one value.
yeah
Do you understand now?
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?
https://www.youtube.com/watch?v=1OuRhD7FmTA&list=PLzMcBGfZo4-mFu00qxl0a67RhjjZj3jXm&index=12&pp=iAQB just watch this basic function calls and how it is done ...
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...
Alrighty
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.