Fraction Calculator Help

This is the place for queries that don't fit in any of the other categories.

Fraction Calculator Help

Postby Guitarman2010 » Fri Apr 05, 2013 10:50 pm

Hello community, I have been using Python for 4 months now and learning something new about it everyday. I do most of my actual coding on Android devices for Android devices. I'm the first to admit that I'm not as smart as some people and trying to learn something like this is very confusing at times. I like to write simple programs that I can actually use on a semi-daily basis like calculation programs. I am not a student looking for answers and to be honest, I wish I did stay in college. I am a person that loves to make a computer do what I want it to do.

That brings me to my question at hand. There were and still are many instances when I needed to add, subtract, multiply etc. fractions and was too lazy or forgetful to do the calculations on paper. I work in the construction trade (mostly plumbing and electrical) and frequently I deal with fractions regarding measured distances. I have successfully written a very verbose and basic fraction calculator in Python.

The current program just consists of inputting the first fraction, selecting add or subtract, inputting the second fraction and finally it returns the calculation. Pretty simple, right? I recently implemented the Android UI menus to select what the first fraction would be.

Code: Select all
title = 'First Frac Denominator Range'
droid.dialogCreateAlert(title)
droid.dialogSetSingleChoiceItems(['1/2', 'Thirds', 'Fourths', 'Eighths', 'Sixteenths'])
droid.dialogSetPositiveButtonText('Ok')
droid.dialogShow()
result = droid.dialogGetResponse().result
ffdr = droid.dialogGetSelectedItems().result

for num1 in ffdr:
  if num1 == 0:
    showfrac = str('1/2')
    frts() #frts() is a function that just displays the showfrac str
    firstot = float(.50)
  elif num1 == 1:
    title = 'Thirds'
    droid.dialogCreateAlert(title)
    droid.dialogSetSingleChoiceItems(['1/3', '2/3'])
    droid.dialogSetPositiveButtonText('Ok')
    droid.dialogShow()
    result = droid.dialogGetResponse().result
    thirds = droid.dialogGetSelectedItems().result

    for tnum1 in thirds:
      if tnum1 == 0:
        showfrac = str('1/3')
        frts()
        firstot = float(.33)
      elif tnum1 == 1:
        showfrac = str('2/3')
        frts()
        firstot = float(.66) 


That is a brief incomplete example of what I have. It coverts whatever fraction into a float. I won't show the entire script because it is long. Then it asks if you want to add or subtract. After that it asks for the second fraction in the same manner but it only deals with 1/2, 3rds, 4ths, 8ths and 16ths. It will either add or subtract the two float values and with a long series of if and elif statements convert the result back to a str.

Example:

Code: Select all
if total_of_two == 0.125:
  answer = str('1/8')
elif total_of_two == .50:
  answer = str('1/2')


Pretty inefficient way of doing it but it works. Ok FINALLY on to my REAL question. How could I incorporate whole numbers to accompany the fraction and calculation? I have read the Python Docs regarding the "fractions" module and I couldn't gather any answer from that. BTW, I'm currently using Python 2.6.2. I tried an if statement that would give me the left over numbers if the total was > 1 but I'm lost when it comes to trying to work with values > 1.

I appreciate any help and I'm not afraid to ask. I was not born with the knowledge of Python in my head lol. Before learning Python I had some experience with BASIC which helped me understand a little about programming but after learning that 13 years ago in high school my brain is rusty. Thanks for reading!
Guitarman2010
 
Posts: 18
Joined: Thu Mar 07, 2013 4:39 pm

Re: Fraction Calculator Help

Postby micseydel » Fri Apr 05, 2013 11:30 pm

Guitarman2010 wrote:Example:

Code: Select all
if total_of_two == 0.125:
  answer = str('1/8')
elif total_of_two == .50:
  answer = str('1/2')

[...]
Ok FINALLY on to my REAL question. How could I incorporate whole numbers to accompany the fraction and calculation?

We'd love to help, but I'm not sure what you want. I don't know what it means to "incorporate whole numbers to accompany the fraction and calculation". Could you elaborate on that?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1301
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Fraction Calculator Help

Postby Guitarman2010 » Fri Apr 05, 2013 11:52 pm

Your right, I should've been more specific. The program as it is only can deal with fractions like ex: 1/8, 3/16, 5/8.......no whole numbers involved. For instance, .50 is the fraction 1/2 or .25 would be 1/4. The total of adding those two values would be .50 + .25 = .75 which is 3/4. Now if I wanted to add 1.50 (1 1/2) + 34.25 (34 1/4)......how would I go about figuring out what to do if the total is greater than 1 (1.00). I have all the if statements to handle float values up to .9375 (15/16) but after that total would exceed 1.00.......that's where I'm lost. I would have the user input any whole numbers first (any number >= 1.0) and secondly input the second part of the fraction like ex: 1/2.

Sorry if I'm not making sense
Guitarman2010
 
Posts: 18
Joined: Thu Mar 07, 2013 4:39 pm

Re: Fraction Calculator Help

Postby micseydel » Sat Apr 06, 2013 12:03 am

My first reaction to this is that Python generously lets us use the modulo operator by 1 on floating point numbers to get the decimal part.
Code: Select all
>>> 34.25 % 1
0.25


If you're not familiar with the modulo operator, I suggest you learn it, it's just a useful tool for being a programming.

Here's some example code for the fractions module that you might find useful
Code: Select all
>>> from fractions import Fraction
>>> myfrac = Fraction(.9375)
>>> myfrac
Fraction(15, 16)
>>> print myfrac
15/16
>>> str(myfrac)
'15/16'
>>> t = Fraction(34.25)
>>> t
Fraction(137, 4)
>>> print t
137/4
>>> '{} {}/{}'.format(t.numerator / t.denominator, t.numerator % t.denominator, t.denominator)
'34 1/4'

Also, it looks like you can remove all your special cases by just using the Fraction class, although do keep in mind that not all numbers can be represented precisely as floating point numbers on computers. You might be able to use the Fraction module for all your operations though, which you might find useful.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1301
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Fraction Calculator Help

Postby Guitarman2010 » Sat Apr 06, 2013 1:27 am

Thanks for replying....your advice has helped me greatly! I see on Python 2.6.2 this example of syntax difference:

Code from 2.7.3
Code: Select all
myfrac = Fraction(.9375)



Code from 2.6.2
Code: Select all
myfrac = Fraction('.9375')
Guitarman2010
 
Posts: 18
Joined: Thu Mar 07, 2013 4:39 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests