Chapter 13: The preprocessor

Program 13.1, Page number: 300

In [1]:
global YES
global NO
YES=1
NO=0


def isEven(number):
        global YES
        global NO

        if(number%2==0):
                answer=YES
        else:
                answer=NO

        return answer

def main():
        global YES
        global NO

        #Calculation/Result
        if(isEven(17)==YES):
                print("YES")
        else:
                print("NO")

        
        if(isEven(20)==YES):
                print("YES")
        else:
                print("NO")


if __name__=='__main__':
        main()
NO
YES

Program 13.2, Page number: 302

In [2]:
global PI
PI= 3.141592654


def area(r):
        global PI
        return (PI*r*r)

def circumference(r):
        global PI
        return (2*PI*r)

def volume(r):
        global PI
        return (1.33 * PI*r*r*r)
        

def main():
        print("radius=1   {0:5}   {1:5}   {2:5}".format(area(1),circumference(1),volume(1)))
        print("radius=4.98   {0:5}   {1:5}   {2:5}".format(area(4.98),circumference(4.98),volume(4.98)))


if __name__=='__main__':
        main()
radius=1   3.141592654   6.283185308   4.17831822982
radius=4.98   77.9127544563   31.2902628338   516.047337866

Program 13.3, Page number: 314

In [3]:
global QUARTS_PER_LITER
QUARTS_PER_LITER=1.05687

def main():
        global QUARTS_PER_LITER                         #Global Reference

        print("***Liters to galons***")
        print("Enter the number of Litres")
        liters=12                                       #liters=raw_input()

        gallons=float(liters)*QUARTS_PER_LITER/4.0      #calculation

        print("{0} Liters = {1} gallons".format(liters,gallons))

if __name__=='__main__':
        main()
***Liters to galons***
Enter the number of Litres
12 Liters = 3.17061 gallons