CSC 131 – Python
CSC 131 – Python
Assignment 3 Solutions
Problem 1:
#variables: deposit – initial deposit
# interest – year interest
# total – base + interest
# countYear – counter for the amount of years
deposit, interest = input (“Please enter initial deposit and interest“)
if ( deposit >=1000000):
print “you are a millionaire already!!”
else:
total = deposit + deposit *interest
countYear = 1
while( total < 1000000):
total = total * interest + total
countYear = countYear + 1
print “ the amount of years that will take to be a millionaire is “, countYear
Problem 4
#short version – 35 days , 1 diet
#variables: weight – current rat weight
# sumWeight – sum of all weights for 35 days
# average – the average weight ( sumWeight/35)
# counter – to clunt amount of days
from __future__import division
sumWeight = 0
counter = 0
while counter < 35:
weight = input (“Please enter the weight of rat “)
sumWeight=sumWeight + weight
counter = counter +1
average = sumWeight/35
print “average weight for 35 days is “, average
Problem 4
#long version 5 weeks 5 diets
#variables: count – counter to count 7 days per week, weight – weight of the rat
#average1, average2, average3, average4, average5 – average for each week
#sumWeight – sum of weights per 7 days, finalAverage – average for 5 weeks
from __future__import division
sumWeight = 0
count = 0
while count < 7:
weight = input (“please enter the weight “)
sumWeight = sumWeight + weight
count = count +1
average1 = sumWeight/7
sumWeight = 0
count = 0
while count < 7:
weight = input (“please enter the weight “)
sumWeight = sumWeight + weight
count = count +1
average2 = sumWeight/7
sumWeight = 0
count = 0
while coun < 7:
weight = input (“please enter the weight “)
sumWeight = sumWeight + weight
count = count +1
average3 = sumWeight/7
sumWeight = 0
count = 0
while coun < 7:
weight = input (“please enter the weight “)
sumWeight = sumWeight + weight
count = count +1
average4 = sumWeight/7
sumWeight = 0
count = 0
while coun < 7:
weight = input (“please enter the weight “)
sumWeight = sumWeight + weight
count = count +1
average5 = sumWeight/7
finalAverage = (average1+average2 +average3+average4+average5)/5
print finalAverage
Problem 8
#variables: num – input number
# count – counter for all 10 numbers
# countPos – counter for positive numbers
countPos = 0
count = 0
while count < 10:
num = input ( “Please enter the number “)
if (num > 0):
countPos = countPos+1
count = count + 1
print “the amount of positive numbers is “, countPos
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.