Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Problem Description:
- In this program we are going to explore a list or an array of integers using Python. Your program should read a set of integers from the user and store them in a list. Read numbers from the user until the user enters 0 to quit. Then store them in a list. Your program should them compute the sum of all the numbers in the list and output to the user. You will use lists and functions to accomplish this task. You program should also give an error message and exit gracefully if the user enters anything other than a number.
- Inputs
- The Python program should accept a set of integers (only numbers) until the user enters 0 to quit.
- Output
- The program should print the sum of all the integers that you have read and stored in a list.
- Approach
- =================== OUTPUT =====================
- Please enter a number: 4
- Please enter a number: 5
- Please enter a number: 6
- Please enter a number: 7
- Please enter a number: 8
- Please enter a number: 0
- 4 + 5 + 6 + 7 + 8 = 30
- 
- *************** THIS IS MY CODE ********************************
- total = 0
- #function to create the list
- def createList():
- myList = []
- return myList
- #function to add values to the list
- def fillList(myList):
- number = int(input("Please enter a number: "))
- myList.append(number)
- return myList
- #function to sum the list
- def sumlist (mylist):
- for number in mylist
- total = total + number
- print (total, " ")
- #function to print the list
- #def printList(myList):
- #for number in myList:
- #print(total, " ")
- #main function
- def main():
- myList = createList()
- fillList(myList)
- sumlist(myList)
- #call the main
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement