# Question on if, while, for

Steve Holden sholden at holdenweb.com
Mon Feb 11 15:09:18 CET 2002

```"Fred" <c at tech.usd253.org> wrote in message
> Hello,
>
> I have written a simple program that I want to simplify. I used if and
> elif. But I am sure this can be simplified with for or while. Or what
> if I wrote a function to do the testing?
>
> Please DO NOT re-write this for me I want to figure it out for myself.
> I would just like to know what is the BEST way to do it:
>
> 1) For loop (nested??)
> 2) While loop
> 3) Function
> 4) Leave it alone it's fine??
>
> I am a rookie here learning Python with my son, very impressed so far!
> Fred
> -----------------------------------------------------------------------
>
> num = input("Enter the numerator: ")
> den = input("Enter the denominator: ")
>
> if num > den: #Test for improper fraction.
>     i = num/den #This will only return the whole part, it discards
> the remainder.
>     j = num%den #This will return only the remainder.
>
>     if j != 0: #If j is equal to 0 you will have a whole number, no
> remainder,
> #skips down to else.
>
> #If j is not equal to 0, begin test to find lowest terms of the
> fraction.
>         if j%9 == 0 and den%9 ==0:
>             a = j/9
>             b = den/9
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
> #Prints the value of i, the whole part, reduces the fracton to lowest
> terms and prints it.
>
>         elif j%8 == 0 and den%8 ==0:
>             a = j/8
>             b = den/8
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>         elif j%7 == 0 and den%7 ==0:
>             a = j/7
>             b = den/7
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>         elif j%6 == 0 and den%6 ==0:
>             a = j/6
>             b = den/6
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>         elif j%5 == 0 and den%5 ==0:
>             a = j/5
>             b = den/5
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>         elif j%4 == 0 and den%4 ==0:
>             a = j/4
>             b = den/4
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>         elif j%3 == 0 and den%3 ==0:
>             a = j/3
>             b = den/3
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>         elif j%2 == 0 and den%2 ==0:
>             a = j/2
>             b = den/2
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>         elif j%1 == 0 and den%1 ==0:
>             a = j/1
>             b = den/1
>             print "You entered an improper fraction, simplified it is:
> %d %d/%d" % (i,a,b)
>
>     else:
>         print "You entered an improper fraction, simplified it is: %d"
> % (i)
> #You have a whole number, which is stored in i, this prints it.
>         #If you typed in 4/2 you would be here.
>
>
> #This section is for a proper fraction.
> elif num%9 == 0 and den%9 ==0:
>     a = num/9
>     b = den/9
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%8 == 0 and den%8 ==0:
>     a = num/8
>     b = den/8
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%7 == 0 and den%7 ==0:
>     a = num/7
>     b = den/7
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%6 == 0 and den%6 ==0:
>     a = num/6
>     b = den/6
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%5 == 0 and den%5 ==0:
>     a = num/5
>     b = den/5
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%4 == 0 and den%4 ==0:
>     a = num/4
>     b = den/4
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%3 == 0 and den%3 ==0:
>     a = num/3
>     b = den/3
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%2 == 0 and den%2 ==0:
>     a = num/2
>     b = den/2
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),
>
> elif num%1 == 0 and den%1 ==0:
>     a = num/1
>     b = den/1
>     print "The fraction you entered is %d/%d in lowest terms." %
> (a,b),

Sorry about my mail program wrpaaing your code. Just some basic hints, then
it's up to you.

1. You could make the whole program repeat by using a "while 1:" loop which
you break out of when the user hits RETURN without making any inputs. This
loop would effectively go around the logic you already have.

2. Whenever you see the same code repeated several times with minor
variations you should think about building a loop that repeats the same
code, varying things as necessary.

3. There are arithmetical algorithms for finding the "LCD" (lowest common
denominator) of two numbers. If you worked out this value on the numerator
and denominator entered by the user, you would easily find whether the
fraction was in its lowest terms.

4. As you build up experience in programming, you will often find better
ways to do things you have already done in inefficient ways. Don't worry in

5. You are to be congratulated on building a working program of any kind. If
more parents took a similar interest in their kids' education it wouldn't
hurt. You just don't have to mind when the kids start writing code five
times as fast as you!

comp.lang.python is a pretty friendly group overall. Welcome, enjoy.

regards
Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, Python Web Programming: http://pydish.holdenweb.com/pwp/

"This is Python.  We don't care much about theory, except where it
intersects with useful practice."  Aahz Maruch on c.l.py

```