The goal of this assignment is to practice processing strings using string functions. For a detailed discussion of string processing see Chapter 8 of How to Think Like a Computer Scientist. For a brief summary of string string processing, see Python Strings Tutorial. You will write a program that adds fractions mathematically as humans do, that is, by finding a common denominator - no converting to and from decimal. Ask the user to enter the fractions as strings using the following notation:
You will have to check the string 'xxx/yyy' to make sure it is a valid fraction. If the fraction isn't valid, you have to tell the user how the fraction is formatted improperly and give them an opportunity to correct the error. Here is a sample run.
Write a function add_frac(n1,d1,n2,d2) that adds the fractions and outputs the answer as a fraction in simplest form (see Euclid's Algorithm below). Your function must pass the following doctests.
It is permissible to output improper fractions (e.g. 7/5) as long as there is no common factor between the numerator and the denominator. Look up Euclid's Algorithm for finding the greatest common factor for two integers. Implement a function gcf(num, den) that returns the greatest common factor between the numerator and denominator and use this function to express the answer in simplest form. Use the following doctests to verify your gcf() function is working. >>> gcf(120, 36) 12 >>> gcf(182664, 154875) 177 Study GuideSample code for the Fractions project is attached below. C-Level Questions will require you to understand
C-level questions only require you to understand simple code fragments and write their output. You do not need to write any code for the C-level questions. B- and A-Level Questions will require you understand
B- and A-level questions require you to not only understand code fragments and write their output, but also may ask you to write short code fragments from scratch that meet the question's requirements. The Python Tutorials page is a great place to start for your studying. There's also the unofficial textbook, How to Think Like a Computer Scientist. |
Python > Python Mini Projects >