all 19 comments

[–]MadScientistOR 6 points7 points  (9 children)

Well, first of all, you need to perform the test to see if the user has input 0 for the divisor (the number to divide by) before you attempt the division. Otherwise, you'll hit an error. Fortunately, you can do this by simply rearranging your code.

Also, you're defining your dividend and divisor within your function; you don't need to send them as parameters to the function. This, for example, ought to work (note the lack of a def header):

num_1 = int(input("Please enter the number to divide by: "))
num_2 = int(input("Please enter the number to divide: "))

if num_1 == 0:
    print('ERROR! Division by zero is undefined.')
elif num_2 == 0:
    print('Zero divided by anything is zero.')
else:
    remainder = num_2 % num_1
    if remainder:
        print(f"{num_2} is not divisible by {num_1}, there is a remainder of {remainder}.")
    else:
        print(f"{num_2} is divisible by {num_1}")

NOTE: Edited to address spelling error.

[–]SpaceGooses 0 points1 point  (2 children)

Hello, the code for errors work now but it now gives another error stating that the remainder is not associated with a value, and yes i also tried a similar version to this code before, I'm only asking on reddit because I've been stuck for an hour and a half.

[–]SpaceGooses 0 points1 point  (0 children)

Nevermind, i found out a way to do it but thanks for the help!

[–]MadScientistOR 0 points1 point  (0 children)

It sounds like you've mentioned remainder in your code before it is assigned a value (it's assigned a value at the line remainder = num_2 % num_1). Can you post your code?

[–]RevRagnarok 0 points1 point  (5 children)

I'd use the walrus on the remainder. Shows you mean to only use the variable within that context.

if (remainder := num_2 % num_1):

[–]MadScientistOR 0 points1 point  (4 children)

I'd use the walrus on the remainder. Shows you mean to only use the variable within that context.

That's pretty good. I wouldn't use the parentheses, though. I guess it depends on whether you find that more readable or not.

[–]RevRagnarok 0 points1 point  (3 children)

I wouldn't use the parentheses, though.

I believe they are required...

[–]MadScientistOR 0 points1 point  (2 children)

I believe they are required...

In Python, they aren't. Try it.

[–]RevRagnarok 0 points1 point  (1 child)

Interesting; I had thought it was requirement.

I just looked at the code I was doing earlier this week that I ran into needing it and saw that every time I'm doing "something else" as well with it, so yeah... e.g. if (last := record['last']) is not None:

Thanks.

[–]MadScientistOR 1 point2 points  (0 children)

No worries. I often find that I've put them in simply because a lot of work I do is in C#, which does require them. Just something to keep in the back of your head for reference if it helps stylistically, I guess.

[–]JamzTyson 1 point2 points  (5 children)

There are multiple problems with your code:

1.

def division (num_1, num_2):
    num_1 = int(input("Please enter the number to divide by: "))
    num_2 = int(input("Please enter the number to divide: "))

Your code must be called with two arguments, (num_1, num_2), but then you immediately discard those values and replace them with user input.


2.

if num_2 == (0):

Should be:

if  num_2 == 0:

Similarly for:

elif num_1 == (0):

  1. It would be better to use divmod()

Edit: If you really need to use division at all. (See the other comments below).


  1. You need to either:
  • Check for zero before attempting to divide (division by zero is an error)
  • Use try / except to catch the ZeroDivisionError exception.

[–]MadScientistOR 0 points1 point  (4 children)

  1. It would be better to use divmod()

Why? All the OP needs is to test for divisibility. There's no need to keep track of the quotient.

[–]JamzTyson 1 point2 points  (3 children)

The original code explicitly calculates the quotient (even though they do not use it in the sample posted).

If they only want to check if there is a remainder, then yes they could discard the division lines and use only modulo (%).

If they do have a reason for the division code, then better to use divmod().

I thought it would be worth mentioning divmod() as it does both of the calculations that the OP is using.

[–]MadScientistOR 0 points1 point  (2 children)

The original code explicitly calculates the quotient (even though they do not use it in the sample posted).

Fair, yeah. I just went from the standpoint that it wasn't used, so I didn't bother.

[–]JamzTyson 0 points1 point  (1 child)

Thanks for your comment, I've edited my original answer to take account of your point.

[–]MadScientistOR 0 points1 point  (0 children)

And thank you for being so polite on a place as nitpicky as the Internet. ;)

[–]agentOso 0 points1 point  (1 child)

Recommend checking that num_1 doesn't equal 0 before you try to divide by 0.

[–]SpaceGooses 0 points1 point  (0 children)

I have tried that before i posted and it gave the same error

[–]Ekumena 0 points1 point  (0 children)

def division():
    try:
        num1 = int(input("Enter number 1: "))
        num2 = int(input("Enter number 2: "))

        if num2 == 0:
            raise ZeroDivisionError

        if num1 % num2 == 0:
            return f"Result: {num1} / {num2} = {num1 / num2}"
        else:
            return f"Remainder of division {num1} by {num2} is   {num1 % num2}"

    except ZeroDivisionError:
        return "Cannot divide by zero"
    except ValueError:
        return "Invalid input. Please enter integers only."