Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Assignment 3 -Python

 

Write a python
program, with the following functions, please call these functions in your
program and test them with the given test cases.

1. Write a Python function to sum all the numbers in a
list.     Sample List : (18, 2, 13, 0, 7)    Expected
Output : 40

2. Write a Python function to reverse a string.  Sample String : “abcd2er4” Expected Output : “4re2dcba”

Answer-Part 1

##This function is used to calculate the sum of the element of any given list
def sum(list):
    total = 0
    for numbers in list:
        total += int (numbers)
    return total



## Accept the elements of the list from the user example 18,2,13,0,7
input_string = input("Enter a list element separated by comma ,  ")
list  = input_string.split(",")

## Display the sum of the elements in the given list
print("Sum of items in the list is:",sum(list))

Part 2

## A function to reverse the letters in a word
def reverse(word):
    reversedWord = ""
    ##Go through each letter in the input word
    for ch in word:
        reversedWord = ch + reversedWord

    return reversedWord

##Accept input from user
word = input("Enter a word: ")

##Display results
print("The reversed word is " + reverse(word))

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!