Lab 4

Lab Description:

Chapter 8 – Exercise 1 Write a function that takes a string as an argument and displays the letters backward, one per line.

Chapter 10 – Exercise 3 Write a function that takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i+1 elements from the original list. For example, the cumulative sum of [1, 2, 3] is [1, 3, 6].

Code:

print (“———-STRING———-“)
name = raw_input (‘enter a name: ‘)
index = len(name)-1
while index >= 0:
letter = name[index]
print letter
index = index – 1
print (“———-List———-“)
number1 = input (‘enter the 1st number: ‘)
number2 = input (‘enter the 2nd number: ‘)
number3 = input (‘enter the 3rd number: ‘)
number4 = input (‘enter the 4th number: ‘)
numbers = [number1, number2, number2, number4]
def sigma(numbers):
sums = []
total = 0
for i in numbers:
total += i
sums.append(total)
print sums

sigma(numbers)

Screenshots: