Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # ------------------------------------------------------------------------------
  4. # Name: Revision Task 8
  5. # Description: Awakening prior knowledge: summarising lists
  6. # Value: 1 point
  7. # Context: Here's a task to perform!
  8. #
  9. # Author: Thomas O'Dell
  10. #
  11. # Created: 27/05/2011
  12. # Copyright: (c) Republic Polytechnic 2011, 2016
  13. # Licence: Not to be copied
  14. # ------------------------------------------------------------------------------
  15. #
  16. # YOUR PROGRAM SHOULD WORK LIKE THIS:
  17. # <code>
  18. # >>> test_function({{my_function}}, ((76, 24, 11, 93, 28, 27, 95, 4, 39, 40, 11, 92),))
  19. # Tester reports function returned value 264, type int.
  20. # Tester reports test completed successfully.
  21. # </code>
  22. #
  23. # <code>
  24. # >>> test_function({{my_function}}, ((19, 88, 43, 15, 95, 89, 68, 72, 9),))
  25. # Tester reports function returned value 228, type int.
  26. # Tester reports test completed successfully.
  27. # </code>
  28. #
  29. # <code>
  30. # >>> test_interactive_task((0,35,3))
  31. # Enter the start value of the range: 0
  32. # Enter the stop value of the range: 35
  33. # Enter the step value of the range: 3
  34. # The sum of all even numbers from 0 to 35 by 3 is 90.
  35. # Tester reports test completed successfully.
  36. # </code>
  37. #
  38. # ------------------------------------------------------------------------------
  39.  
  40.  
  41. # Write a function that
  42. # takes in a list of numbers,
  43. # calculates the sum of all the EVEN numbers in the list
  44. # and returns the sum
  45. #
  46. # (see the "SHOULD WORK LIKE THIS" section for exact input and output format)
  47. #
  48. ###############################################
  49. # YOUR CODE GOES BETWEEN HERE... #
  50. # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv #
  51. def numList(x):
  52. summ = 0
  53. for i in x:
  54. if (i % 2) == 0:
  55. summ += i
  56.  
  57.  
  58. return summ
  59. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #
  60. # ...AND HERE. #
  61. ###############################################
  62.  
  63. if __name__ == '__main__':
  64. # these next lines do automatic testing for you:
  65. from TestMyCode import run_tests
  66. my_name = 'Keith' # put your name here so the tests can run.
  67. my_function = numList # put the name of the function you wrote so the tests can run.
  68. run_tests()
  69.  
  70. # Ask the user for the start, stop and step values of a range.
  71. #
  72. # Call the function you wrote above
  73. # to calculate the sum of all the even numbers in the given range.
  74. #
  75. # Print out the calculated sum.
  76. #
  77. # (see the "SHOULD WORK LIKE THIS" section for exact input and output format)
  78. ###############################################
  79. # YOUR CODE GOES BETWEEN HERE... #
  80. # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv #
  81.  
  82. num1 = int(input("Enter the start value of the range: "))
  83. num2 = int(input("Enter the stop value of the range: "))
  84. num3 = int(input("Enter the step value of the range: "))
  85.  
  86. '''
  87. y = []
  88.  
  89. for i in range(num1, num2, num3):
  90. y.append(i)
  91.  
  92. '''
  93.  
  94. y = range(num1, num2, num3)
  95.  
  96. z = numList(y)
  97.  
  98. print("The sum of all even numbers from " + str(num1) + " to " + str(num2) + " by " + str(num3) + " is " + str(z) + ".")
  99.  
  100. # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #
  101. # ...AND HERE. #
  102. ###############################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement