NBowman6
NBowman6

Reputation: 39

Writing a function that calculates the ratio of two numbers

I am brand new to Python coding, so keep that in mind for the following problem. I am just learning the use of defining functions, arguments, and variables.

Define a function called ratioFunction that takes two numbers called num1 and num2 as arguments and calculates the ratio of the two numbers, and displays the results as (in this example num1 is 6, num2 is 3): ‘The ratio of 6 and 3 is 2’. The output after running the code should look like this:

Enter the first number: 6
Enter the second number: 3
The ratio of 6 and 3 is 2.

So here's what I've cooked up with my limited knowledge of coding and my total confusion over functions:

def ratioFunction(num1, num2):
    num1 = input('Enter the first number: ')
    int(num1)
    num2 = input('Enter the second number: ')
    int(num2)
    ratio12 = int(num1/num2)
    print('The ratio of', num1, 'and', num2,'is', ratio12 + '.')
ratioFunction(num1, num2)

I am utterly confused, any help would be appreciated!

Upvotes: 1

Views: 50812

Answers (5)

akmal_quamri
akmal_quamri

Reputation: 1

def RatioFunctions(num1, num2):
    n1 = float(num1)
    n2 = float(num2)

    return n1/n2

Upvotes: 0

kargarf
kargarf

Reputation: 66

without using division(/) operations, as bellow you can get ration of 2 numbers :)

Output will be as follow: coff num1 / num2

static int GCD(int p, int q)//find greatest common divisor
{
    if (q == 0)
    {
        return p;
    }

    int r = p % q;

    return GCD(q, r);
}
static string  FindRatio(int num1, int num2)
{

    string oran = "";
    int gcd;
    int quotient = 0;
    while (num1 >= num2)
    {
        num1 = num1 - num2;
        quotient++;
    }

    gcd = GCD(num1, num2);

    //without using division finding ration of num1 i1
    int i1 = 1;
    while (gcd*i1 != num1)
    {
        i1++;
    }
    //without using division finding ration of num1 i2
    int i2 = 1;
    while (gcd * i2 != num2)
    {
        i2++;
    }

    oran = string.Concat(quotient, " ", i1,"/",i2);
    return oran;
}

Upvotes: 0

Jorge Plaza
Jorge Plaza

Reputation: 350

There are several minor things to fix in your function:

  1. The function ratioFunction should not receive any parameters, because you are asking the user to input the values for num1 and num2 inside the function.

  2. The statement "int(num1)" (without quotes) doesn't alter by itself the value of num1, it just returns the integer representation of num1. You should assign num1 the returned value, or create another variable for that purpose.

  3. You're mixing strings and ints in the print statement (you cannot add a string and an integer). You could replace the + with a comma, but that's a bit ugly. There are several ways to achieve what you want, but I recommend you to use String formatting:

    print('The ratio of {0} and {1} is {2}.'.format(num1, num2, ratio12))

If you have to consider non-integer divisions, use float (num1)/num2 to calculate it.

Upvotes: 0

Kshitij Saraogi
Kshitij Saraogi

Reputation: 7639

There are primarily 3 issues with your function:

  1. The int division problem:
    In Python3, dividing an int object by another int object can return a float.
    For example:

    In[] : a = 10
    In[] : b = 3
    In[] : a / b
    Out[]: 3.3333333333333335
    
    In[] : a // b # Fix 01: Specifying integer division to the interpreter
    Out[]: 3
    
    In[] : int(a / b)
    Out[]: 3 # Fix 02: Type casting the float to int
    
  2. Catching the return value of type casting num1 and num2.
    You need to store the value returned by the int function on num1 and num2. Better yet, you can make this explicit when you ask the user for input.

    In[] : num1 = int(input("Enter the first number :")) 
    In[] : num2 = int(input("Enter the second number :"))
    
  3. Arguments in function ratioFunction's definition: Since, you are prompting the user for input, it is totally point less to pass them as parameters beforehand. This is a poor design technique and can lead to further issues when you call ratioFunction from other scripts or functions.
    Fix: Remove the arguments, and define it as def ratioFunction()

Tip: I notice that you are using JAVA convention for naming Python function. I would suggest you read PEP8 Python coding style guide.

Upvotes: 0

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 96287

The problem is that you aren't capturing the results of the call to int.

def ratioFunction(num1, num2):
    num1 = input('Enter the first number: ')
    int(num1) # this does nothing because you don't capture it
    num2 = input('Enter the second number: ')
    int(num2) # also this
    ratio12 = int(num1/num2)
    print('The ratio of', num1, 'and', num2,'is', ratio12 + '.')
ratioFunction(num1, num2)

Change it to:

def ratioFunction(num1, num2):
    num1 = input('Enter the first number: ')
    num1 = int(num1) # Now we are good
    num2 = input('Enter the second number: ')
    num2 = int(num2) # Good, good
    ratio12 = int(num1/num2)
    print('The ratio of', num1, 'and', num2,'is', ratio12 + '.')
ratioFunction(num1, num2)

Also, when you call ratioFunction(num1, num2) in your last line, this will be a NameError unless you have num1 and num2 definied somewhere. But honestly, this is totally unecessary because you are taking input. This function has no need for arguments. Also, there will be another bug when you print because you are using the + operator on ratio12 + '.' but ratio12 is an int and '.' is a string. Quick fix, convert ratio12 to str:

In [6]: def ratioFunction():
   ...:     num1 = input('Enter the first number: ')
   ...:     num1 = int(num1) # Now we are good
   ...:     num2 = input('Enter the second number: ')
   ...:     num2 = int(num2) # Good, good
   ...:     ratio12 = int(num1/num2)
   ...:     print('The ratio of', num1, 'and', num2,'is', str(ratio12) + '.')
   ...:

In [7]: ratioFunction()
Enter the first number: 6
Enter the second number: 2
The ratio of 6 and 2 is 3.

Although, likely, your function is suppose to take arguments, and you get input outside the function and pass it to it.

Upvotes: 4

Related Questions