Printing values in lists, and printing them reversed with spacing, intro
to programming class
im doing my homework and ive for the most part accomplished what my
teacher asked for which is the following "Program #1: Write a program that
does the following in the order given:
On a single line, print the data values in reverse order.
On a single line, print the values in the list of divisor sums in reverse
order.
On a single line, print the values in the list of averages in reverse
order to 1 decimal place."
I did the main part of the homework but printing them in reverse, only
numbers, and with certain spacing is confusing me. Heres my code just in
case you wanna see, but i dont think its required, you can skip through
all this :
data = [12, 3, 24, 30, 9, 11, 21, 8] #Hard Coded
sumofdivisors = [ ] #Created empty list to
fill
averageofdivisorpairs = [ ] #Another Empty List
dn = 1 # this is just the
code i wrote so it will
i = 1 # fill the emptylist
titled sumofdivisors
print data # which takes the
divisors of each value
for numbers in data: # and sums them,
adding the total to
sum1 = 0 # the list, printing
the list at the end.
dn = 1 # how do i make the
list print these values
while dn <= numbers: # in reverse and only
the numbers? Im
if numbers % dn == 0 : # thinking it has to
do with negative
sum1 = sum1 + dn # indencies. but im
not sure
dn = dn + 1 #
sumofdivisors.append(sum1) #
print sumofdivisors
i = 0
size = len(sumofdivisors)
truesize = size - 1
sum2 = 0
while i < truesize :
sum2 = sumofdivisors[i] + sumofdivisors[i + 1]
avg = sum2 / float(2)
averageofdivisorpairs.append(avg)
i = i + 1
print averageofdivisorpairs # same here, how do
only the values in reverse
totalsum = 0
size = len(sumofdivisors)
i = 0
while i <= size-1 :
sum3 = sumofdivisors[i]
totalsum = totalsum + sum3
i = i + 1
print "total sum = ", totalsum
totalsum2 = 0
size = len(averageofdivisorpairs)
i = 0
while i <= size - 1 :
sum4 = averageofdivisorpairs[i]
totalsum2 = totalsum2 + sum4
avg2 = float(totalsum2) / size
i = i + 1
print "average of averages = %.2f" % (avg2)
We can only use the len() function and .append() function, no reverse sort
or any of that. The target output is supposed to look like : Your output
should look like this:
8 21 11 9 30 24 3 12
15 32 12 13 72 60 4 28
23.5 22.0 12.5 42.5 66.0 32.0 16.0
total sum = 236
average of averages = 30.64
Mines looks like this, its not reversed, its not only values, and its not
spaced correctly:
[12, 3, 24, 30, 9, 11, 21, 8]
[28, 4, 60, 72, 13, 12, 32, 15]
[16.0, 32.0, 66.0, 42.5, 12.5, 22.0, 23.5]
total sum = 236
average of averages = 30.64
Help would be appreciated. Thanks
No comments:
Post a Comment