Advertisement
scorpion2018

Untitled

Nov 25th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. Problem Description:
  2. 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.
  3. Inputs
  4. The Python program should accept a set of integers (only numbers) until the user enters 0 to quit.
  5. Output
  6. The program should print the sum of all the integers that you have read and stored in a list.
  7. Approach
  8.  
  9. =================== OUTPUT =====================
  10. Please enter a number: 4
  11. Please enter a number: 5
  12. Please enter a number: 6
  13. Please enter a number: 7
  14. Please enter a number: 8
  15. Please enter a number: 0
  16. 4 + 5 + 6 + 7 + 8 = 30
  17. *************** THIS IS MY CODE ********************************
  18. total = 0
  19.  
  20. #function to create the list
  21. def createList():
  22. myList = []
  23. return myList
  24.  
  25. #function to add values to the list
  26. def fillList(myList):
  27. number = int(input("Please enter a number: "))
  28. myList.append(number)
  29. return myList
  30.  
  31. #function to sum the list
  32. def sumlist (mylist):
  33. for number in mylist
  34. total = total + number
  35. print (total, " ")
  36.  
  37. #function to print the list
  38. #def printList(myList):
  39. #for number in myList:
  40. #print(total, " ")
  41.  
  42. #main function
  43. def main():
  44. myList = createList()
  45. fillList(myList)
  46. sumlist(myList)
  47.  
  48. #call the main
  49. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement