Chapter 6: Loops

Example square.c, Page 102

In [3]:
def main():
    print "This program prints a table of squares."
    n=int(raw_input("Enter number of entries in table: ")) #input number of entries
    i=1 #variable declaration
    while(i<=n):
        print "%10d%10d" % (i,i * i) #printing number and it's square
        i=i+1
if  __name__=='__main__':
    main()
This program prints a table of squares.
Enter number of entries in table: 5
         1         1
         2         4
         3         9
         4        16
         5        25

Example sum.c, Page 103

In [6]:
def main():
    sum=0
    print "This program sums a series of integers."
    n=int(raw_input("Enter integers (0 to terminate): ")) #input the integers to operate on
    while(n!=0):
        sum=sum+n #calculating sum till 0 encountered
        n=input()
    print "The sum is: %d" % sum #printing sum
if  __name__=='__main__':
    main()
This program sums a series of integers.
Enter integers (0 to terminate): 8
23
71
5
0
The sum is: 107

Example numdigits.c, Page 105

In [7]:
def main():
    digits=0 #initialise
    n=int(raw_input("Enter a nonnegative integer: ")) #input the number
    n=n/10
    digits=digits+1
    #finding number of digits
    while(n>0):
        n=n/10
        digits=digits+1
    print "The number has %d digit(s)." % digits #printing answer

if  __name__=='__main__':
    main()
Enter a nonnegative integer: 60
The number has 2 digit(s).

Example square2.c, Page 110

In [12]:
def main():
    print "This program prints a table of squares." 
    n=int(raw_input("Enter number of entries in table: ")) #input number of entries
    for i in range (1,(n+1)):
        print "%10d%10d" % (i,i * i) #printing number and it's square
        
if  __name__=='__main__':
    main()
This program prints a table of squares.
Enter number of entries in table: 5
         1         1
         2         4
         3         9
         4        16
         5        25

Example square3.c,Page 110

In [14]:
def main():
    print "This program prints a table of squares." 
    n=int(raw_input("Enter number of entries in table: ")) #input number of entries
    
    #variable declaration
    i=1
    odd=3
    square=1
    #calculation
    for i in range (1,(n+1)):
        print "%10d%10d" % (i,square)
        square=square+odd 
        odd=odd+2
        
if  __name__=='__main__':
    main()
This program prints a table of squares.
Enter number of entries in table: 5
         1         1
         2         4
         3         9
         4        16
         5        25

Example checking.c, Page 115

In [17]:
def main():
    balance=0.0 #initialise
    print "*** ACME checkbook-balancing program ***"
    print "Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit"
    print ""
    while(1):
        cmd=int(raw_input("Enter command: ")) 
        
        #operate on balance according to the option selected
        if cmd==0:
            balance=0.0 #clear balance
        elif cmd==1:
            credit=float(raw_input("Enter amount of credit: "))
            balance=balance+credit #credit
        elif cmd==2:
            debit=float(raw_input("Enter amount of debit: "))
            balance=balance-debit #debit
        elif cmd==3:
            print "Current balance: $%0.2f" % balance #show current balance
        elif cmd==4:
            return 0 #exit
        else:
            print "Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit" 
            
        
if  __name__=='__main__':
    main()
*** ACME checkbook-balancing program ***
Commands: 0=clear, 1=credits, 2=debit, 3=balance, 4=exit

Enter command: 1
Enter amount of credit: 1042.56
Enter command: 2
Enter amount of debit: 133.79
Enter command: 1
Enter amount of credit: 1754.32
Enter command: 2
Enter amount of debit: 1400
Enter command: 2
Enter amount of debit: 68
Enter command: 2
Enter amount of debit: 50
Enter command: 3
Current balance: $1145.09
Enter command: 4
In [ ]: