2

I have to write a program that converts an improper fraction to a mixed number. Prompt the user for the numerator and the denominator, then calculate and display the equivalent mixed number. numerator is 23 and denominator is 6.

This is what I have so far...

num = int(input('Type numerator'))
dem = int(input('Type denominator'))

I'm not exactly sure what the next step is...I know the answer is supposed to be The mixed number is 3 and 5/6.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
mrsga16eats
  • 41
  • 3
  • 8

3 Answers3

3

Assuming that your inputs are always integer values, you can use the divide and mod operators to do this.

The following should work:

a = num // dem
b = num % dem
print 'The mixed number is {} and {}/{}'.format(a, b, dem)
Jenner Felton
  • 787
  • 1
  • 9
  • 18
  • Is there a way to get this to reduce fractions? I see 6, 4 for example will result in 1 and 2/4 – EoinS May 22 '16 at 05:42
  • 1
    @EoinS You could find the greatest common denominator and then divide both numbers by that value. It's a bit of extra code, but shouldn't be too tough to add. I like your solution as it handles this, but it depends whether or not they want to use separate modules. – Jenner Felton May 22 '16 at 05:51
  • good point i am on mobile and had to try 5 online interpreters before finding one that would accept fractions module :( – EoinS May 22 '16 at 05:54
  • This is how I have it written and it is still coming back with an error. Can you see where I've messed up? def main(): num = int(input('Enter the numerator')) den = int(input('Enter the denominator:')) a = num // dem b = num % dem print ('The mixed numer is {} and {}/{}', format(a,b, dem)) main () – mrsga16eats May 22 '16 at 13:29
1

Good question. Here's one solution using Fraction function. Fraction is nice because it reduces fractions. You use floor divide (//) to strip out the whole number and then feed the remaining fraction to Fraction:

From fractions import Fraction
num = int(input('Type numerator'))
dem = int(input('Type denominator'))
Print str(num // dem) + ' and ' + str(Fraction(num%dem,dem)) if num//dem != 0 else str(Fraction(num%dem,dem))

[Python 3.5] (https://docs.python.org/2/library/fractions.html#fractions.Fraction) Extended reading on Fraction. Because you feed it num and dem rather than a pure decimal, it is pretty fail-safe.

This also gets rid of response of '0 and ...' which was bothering me.

Without using fractions module, we have to find the greatest common divider (borrowing gcd function from fractions) reduce our initial fraction and then use brilliant solution from @Jenner Felton

def gcdm(num,dem):
  while dem:
    num, dem = dem, num%dem
  return num
gcd = gcdm(num,dem)
num, dem = num/gcd, dem/gcd
Print "%d and %d/%d" % ((num//dem), (num%dem),dem) if num//dem !=0 else "%d/%d" % (num%dem,dem)
EoinS
  • 5,405
  • 1
  • 19
  • 32
0

As the other answers point out, you can use the integer division and modulo operators to get the numbers you want.

Another aspect to coding this problem that will make things easier is creating a while loop along with a try, except block for the entry of the improper fraction so that you do not get exceptions.

It would look like this:

while True:
    try:
        num = int(input('Type numerator'))
        break
    except:
        continue

Split this off into a separate function to make things a bit nicer:

def get_data(message, f = lambda data: data):
    '''Prompts the user for data entry. 
       Function f is applied to data; any exception encountered
       results in user being prompted again.
       Entry is simply returned if no function is supplied.'''
    message = str(message)
    while True:
        try:
            num = f(input(message))
            break
        except:
            continue
    return num

num = get_data('Type numerator', int)
den = get_data('Type denominator', int)

Here's my one-liner for obtaining the mixed number:

'%s%s%s' % ('%s ' % (num//den) if num//den != 0 else '', '%s/' % (num%den) if num%den != 0 else '', '%s' % den if num%den != 0 else '')
Rick
  • 43,029
  • 15
  • 76
  • 119