-1
def mult(num):
    #num='(1,2)(3,4)'
    numn=int(num[1])
    denom=int(num[3])
    numn1=int(num[6])
    denom1=int(num[8])
    numnetor=numn*numn1
    denominetor=denom * denom1
    ans=str(numnetor)+'/'+ str(denominetor)
    return ans
askUser=input("Enter Fraction:")
print(mult(askUser))

I tried to do it this way but when I input double digit number it crashes What can I do?

idjaw
  • 25,487
  • 7
  • 64
  • 83
  • 1
    A string of a tuple, are you sure you need to play with fire like that? – Brian Oct 17 '15 at 01:04
  • You say they're "passed down as a tuple", but they aren't, they're passed in as a string. That means `num[1]` isn't taking a number from tuple, it's taking the second character from the string. (And if it was being passed in as a list of tuples, it wouldn't work because num[1] would be the second tuple and num[3/6/8] would error). – TessellatingHeckler Oct 17 '15 at 01:04
  • How can i change my code to do that to input two tuple like this (12,10)(40,2) and then multiply them.@ TessellatingHeckler – Ezana Tesfaye Oct 17 '15 at 01:11
  • Is there something wrong with using the `fractions` module as i did in my answer? – pppery Oct 17 '15 at 01:14

2 Answers2

1

Python already has support for fractions in the standard library. If your fraction is in string form:

from fractions import Fraction
f = Fraction(askUser)

If your fraction is in tuple form:

f = Fraction(*fraction_tuple)

The resulting Fraction objects can be used in standard arithmetic as if they were numbers.

pppery
  • 3,731
  • 22
  • 33
  • 46
  • i didn't understand your answer @pperry – Ezana Tesfaye Oct 17 '15 at 01:16
  • What's ambiguous about it? @EzanaTesfaye, you don't need to say `@ppperry` when talking in the comments on my answer, as it automatically notifies me. – pppery Oct 17 '15 at 01:21
  • oh ok The answer you gave divides it the thing i wanted to do is ask the user for an input like this (10,2)(12,4) and multiply the two fractions-> 10/2 * 12/4=120/8 – Ezana Tesfaye Oct 17 '15 at 01:25
0

Expressing my doubt that you will actually need to process strings of compound tuples, the solution you are looking for is raw_input (instead of input), and ast (which is what input does).

Now, when the two tuples are understood separately, the program may now process numbers with more than two digits.

import ast

def mult(num):
    sep_indx = num.find(')(')
    assert sep_indx > -1

    tuple1, tuple2 = num[:sep_indx+1], num[sep_indx+1:]
    tuple1 = ast.literal_eval(tuple1)
    tuple2 = ast.literal_eval(tuple2)

    numn=int(tuple1[0])
    denom=int(tuple1[1])
    numn1=int(tuple2[0])
    denom1=int(tuple2[1])
    numnetor=numn*numn1
    denominetor=denom * denom1
    ans=str(numnetor)+'/'+ str(denominetor)
    return ans

askUser=raw_input("Enter Fraction:")
print(mult(askUser))

#Enter Fraction:(1,20)(3,40)
#3/800

Do note that your program will still have problems with division by zero errors, issue with extra spaces, and any non-integer numbers entered by the user. I am leaving these to you.

Brian
  • 7,394
  • 3
  • 25
  • 46
  • The OP could be using Python 3. Also, what is the advantage of this over the `fractions` module? – pppery Oct 17 '15 at 01:28
  • Nothing is wrong with using fractions for normal problems. I am just answering the question. – Brian Oct 17 '15 at 01:29
  • Wow @ppperry [this is ridiculous](http://stackoverflow.com/a/954840/1558430), thanks for the tip! – Brian Oct 17 '15 at 01:32