Question on if, while, for

Fred c at tech.usd253.org
Mon Feb 11 14:45:09 CET 2002


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),



More information about the Python-list mailing list