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 using the Android UI menus to select what the first fraction would be.

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.Code: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)

Example:

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.Code:if total_of_two == 0.125: answer = str('1/8') elif total_of_two == .50: answer = str('1/2')

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!

Tweet This+ 1 thisPost To Linkedin