0

When I testing division with x = 5 , y = 32 so, division function does not working.

This is impossible: '1/0' , in that I added 'try & except' after it 'return float(x) / float(y)' not working.

import math

def control(a,x,y,z,k):
    return {
        'ADDITION': addition(x, y),
        'SUBTRACTION': subtraction(x, y),
        'MULTIPLICATION': multiplication(x, y),
        'DIVISION': division(x, y),
        'MOD': modulo(x, y),
        'SECONDPOWER': secondPower(x),
        'POWER': power(x, y),
        'SECONDRADIX': secondRadix(x),
        'MAGIC': magic(x, y, z, k)
    }
def addition(x, y):
    return float(x) + float(y)

def subtraction(x, y):
    return float(x) - float(y)

def multiplication(x, y):
    return float(x) * float(y)

def division(x, y):  

          if(y != 0):
               return float(x) / float(y)

          else:
               raise
                   return ValueError("This operation is not supported for given input parameters")
            if (x!= 0 or y!= 0): 
                     return float(x) / float(y)

def modulo(x, y):
        if (x % 2) == 0:
                 print("This operation is not supported for given input parameters")
        else:
                return float(x) % float(y)

def secondPower(x):
    return math.pow(float(x),2.0)

def power(x, y):
    return math.pow(float(x),float(y))

def secondRadix(x):
    return math.sqrt(float(x))


def magic(x, y, z, k):
    l = float(x) + float(k)
    m = float(y) + float(z)


try:
    control(a,x,y,z,k)
except ValueError:
    print("This operation is not supported for given input parameters")

out = control(a,x, y, z, k)
print(out)

Where I'm doing wrong in this code? I'm beginner in Python.

Husni Salax
  • 1,968
  • 1
  • 19
  • 29
  • Please edit your question to include the exact error output you get when running this. – shuttle87 Nov 17 '15 at 21:45
  • 3
    You are using `try / except` the wrong way. Just put `return float(x) / float(y)` inside the `try` and let the exception be raised if it can not work. – Delgan Nov 17 '15 at 21:45
  • also, you are initiating everything with `float()` which will return `0.0` – R Nar Nov 17 '15 at 21:46
  • In `division(x, y)`, You're writing `x = 0` and `y = 0`, which replaces the values passed as arguments. – Jérôme Nov 17 '15 at 21:46

1 Answers1

0

There's a few bugs you need to fix here. First of all you overwrite the parameters in the division function:

def division(x, y):  
        try:
            x = 0  #now x is 0 regardless of what you passed in
            y = 0  #same for y

Then in this line:

if (x!= 0 or y!= 0): 

If x is not 0 this is true because the or operation will return true if any of the conditions are true. So for example if you have x = 1 and y = 0 you still perform the invalid division. You could fix this by checking just for y != 0.

A much better way to do this would just be to let python do the division and catch the ZeroDivisionError that is raised if you attempt to make an invalid division:

def division(x, y):
    try:
        return x/y
    except ZeroDivisionError:
        print("This operation is not supported for given input parameters")

This is much more in line with the overall Python philosophy of "Easier to ask for forgiveness than permission".

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
  • Great answer with a great explanation. –  Nov 17 '15 at 21:56
  • @shuttle87 I tried it : def division(x, y): try: x/y except ZeroDivisionError: print("This operation is not supported for given input parameters") but it's not working (( I'm getting error : Division function is not working for: x=5, y=32 – Husni Salax Nov 17 '15 at 21:56
  • @HusniddinSalahiddinov It was just missing a return, you can see here that it works though: http://ideone.com/OHFbCm – shuttle87 Nov 17 '15 at 22:02
  • @shuttle87 I've added in try 'return x/y' so, I'm getting error: Division function is not working for: x=5, y=0 (( – Husni Salax Nov 17 '15 at 22:12
  • I can't reproduce the problem: http://ideone.com/ANsEQH – shuttle87 Nov 18 '15 at 00:05