Difference between revisions of "CSC111 Homework 2 Solutions 2014"
(→Problem 3) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 200: | Line 200: | ||
==Output== | ==Output== | ||
− | < | + | <source lang="text"> |
+--------------------+ | +--------------------+ | ||
Line 257: | Line 257: | ||
| Doc | Doc | | | Doc | Doc | | ||
+----------------------------------------+ | +----------------------------------------+ | ||
− | </ | + | </source> |
− | |||
<br /> | <br /> | ||
+ | |||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 13:07, 13 March 2015
--D. Thiebaut (talk) 11:20, 20 February 2014 (EST)
Contents
Problem #1
#hw2a.py
#Zoey Jingyi Sun (ch)
#Yipeng Lai (bq)
#Solution Program for Problem#1 in Homework#2
#
#This program is to breakdown the money entered by the user into bills and coins
#-------------------------------------------------------------------------------
#
#Get user input
amount=float(input( "Please enter the amount of money you want to withdraw:"))
#Print top bar
bar="+"+"-"*36+"+"
length=len(bar)
print(bar)
print("|","Total amount withdrawn: ","$%7.2f" % amount," |")
print("|"+" "*(length-2)+"|")
bill=int(amount)
coin=int((amount-bill)*100)
#Calculate bills numbers
BILL1=20
BILL2=10
BILL3=5
BILL4=1
#Calculate the numbers of $20-bill(s)
noBill= bill // BILL1
leftover= bill % BILL1
line1=" %2d" % noBill+" $20-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Calculate the numbers of $10-bill(s)
noBill= leftover // BILL2
leftover= leftover % BILL2
line1=" %2d" % noBill+" $10-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Calculate the numbers of $5-bill(s)
noBill= leftover // BILL3
leftover= leftover % BILL3
line1=" %2d" % noBill+" $5-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Calculate the numbers of $1-bill(s)
noBill= leftover // BILL4
leftover= leftover % BILL4
line1=" %2d" % noBill+" $5-bill(s)"
print ("|"+line1+" "*(length-len(line1)-2)+"|")
#Print middle bar
print ("|"+"-"*19+" "*(length-21)+"|")
#Calculation of coins numbers
COIN1=25
COIN2=10
COIN3=5
COIN4=1
#Calculate the numbers of quarter(s)
noCoin= coin // COIN1
leftover= coin % COIN1
line2=" %2d" % noCoin+" quarter(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Calculate the numbers of dime(s)
noCoin= leftover // COIN2
leftover= leftover % COIN2
line2=" %2d" % noCoin+" dime(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Calculate the numbers of nickel(s)
noCoin= leftover // COIN3
leftover= leftover % COIN3
line2=" %2d" % noCoin+" nickel(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Calculate the numbers of cent(s)
noCoin= leftover // COIN4
leftover= leftover % COIN4
line2=" %2d" % noCoin+" cent(s)"
print ("|"+line2+" "*(length-len(line2)-2)+"|")
#Print bottom bar
print(bar)
Problem #2
#hw2b.py
#Zoey Jingyi Sun (ch)
#Yipeng Lai (bq)
#Solution Program for Problem#2 in Homework#2
#
#This program is to calculate the total balance of an investment growing at
#a fixed interest rate over ten years
#---------------------------------------------------------------------------
#Get user input
initial= float(input("Please enter the initial balance :"))
intRate= float(input("Please enter the interest rate (enter 0.03 for 3%):"))
print()
print("Table for an investment of $ %.2f"%initial,"at a rate of %.1f%%"%(intRate*100))
#Calculate the balance after 1 year
balance=initial*(1+intRate)
print("Balance after 1 year: $%.2f"%balance)
#Calculate the balance after 2 year
balance=balance*(1+intRate)
print("Balance after 2 year: $%.2f"%balance)
#Calculate the balance after 3 year
balance=balance*(1+intRate)
print("Balance after 3 year: $%.2f"%balance)
#Calculate the balance after 4 year
balance=balance*(1+intRate)
print("Balance after 4 year: $%.2f"%balance)
#Calculate the balance after 5 year
balance=balance*(1+intRate)
print("Balance after 5 year: $%.2f"%balance)
#Calculate the balance after 6 year
balance=balance*(1+intRate)
print("Balance after 6 year: $%.2f"%balance)
#Calculate the balance after 7 year
balance=balance*(1+intRate)
print("Balance after 7 year: $%.2f"%balance)
#Calculate the balance for 8 year
balance=balance*(1+intRate)
print("Balance after 8 year: $%.2f"%balance)
#Calculate the balance for 9 year
balance=balance*(1+intRate)
print("Balance after 9 year: $%.2f"%balance)
#Calculate the balance for 10 year
balance=balance*(1+intRate)
print("Balance after 10 years: $%.2f"%balance)
</source>
Problem 3
# hw2c.py
# Sarah Sutto-Plunz (bu)
# Kathleen McLaughlin (aj)
# (Edited by D. Thiebaut)
# 2/12/14
# These programs output various boxes around lists of dwarfnames.
#--------------------------------------------------------------
#homework 1a redone
list = [ "Sleepy", "Sneezy", "Bashful", "Happy", "Grumpy", "Dopey", "Doc"]
for dwarfNames in list:
line = "%-18s" % (dwarfNames)
length = 20
print( "+" + "-" * ( length ) + "+" )
print( "| " + line + " |" )
print( "+" + "-" * ( length ) + "+" )
print()
#homework 1b redone
print('+%s+' % ('-' * 20) )
for dwarfName in list:
print( "|%-20s|" % (dwarfName) )
print('+%s+' % ('-' * 20) )
#homework 1c redone
print('+%s+' % ('-' * 20) )
for dwarfName in list:
print( "|%20s|" % (dwarfName) )
print('+%s+' % ('-' * 20) )
#homework 1d redone
print('+%s+' % ('-' * 40) )
for dwarfName in list:
print( "|%19s | %-18s|" % ( dwarfName, dwarfName ) )
print('+%s+' % ('-' * 40) )
Output
+--------------------+
| Sleepy |
+--------------------+
+--------------------+
| Sneezy |
+--------------------+
+--------------------+
| Bashful |
+--------------------+
+--------------------+
| Happy |
+--------------------+
+--------------------+
| Grumpy |
+--------------------+
+--------------------+
| Dopey |
+--------------------+
+--------------------+
| Doc |
+--------------------+
+--------------------+
|Sleepy |
|Sneezy |
|Bashful |
|Happy |
|Grumpy |
|Dopey |
|Doc |
+--------------------+
+--------------------+
| Sleepy|
| Sneezy|
| Bashful|
| Happy|
| Grumpy|
| Dopey|
| Doc|
+--------------------+
+----------------------------------------+
| Sleepy | Sleepy |
| Sneezy | Sneezy |
| Bashful | Bashful |
| Happy | Happy |
| Grumpy | Grumpy |
| Dopey | Dopey |
| Doc | Doc |
+----------------------------------------+