Lab 4

Lab 4

Lab description: Write a function that takes a string as an argument and displays the letters backward, one per line.

Code:

# Viralkumar Mistry
# date: 3/14/13
# session: 9306
# Lab4: String and list

print(“————String———–“)
# Ask the user to input a name and print the reverse of it
name1 = raw_input(‘Enter a name: ‘)
index = len(name1) – 1
while index >= 0:
x = name1[index]
print x
index = index – 1

 

print(“————-List————“)

# Ask user to enter 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.

def add_all(names):
n = (n1+n2)
na = (n +n3)
nb = (na + n4)
t = [n1,n,na,nb]
return t

n1 = input(‘Enter first number: ‘)
n2 = input(‘Enter second number: ‘)
n3 = input(‘Enter third number: ‘)
n4 = input(‘Enter fourth number: ‘)

names = [n1,n2,n3,n4]

print ‘The cumulative sum of’,names,’is’,add_all(names)

Screen Shot of result: