Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # ------------------------------------------------------------------------------
- # Name: Revision Task 8
- # Description: Awakening prior knowledge: summarising lists
- # Value: 1 point
- # Context: Here's a task to perform!
- #
- # Author: Thomas O'Dell
- #
- # Created: 27/05/2011
- # Copyright: (c) Republic Polytechnic 2011, 2016
- # Licence: Not to be copied
- # ------------------------------------------------------------------------------
- #
- # YOUR PROGRAM SHOULD WORK LIKE THIS:
- # <code>
- # >>> test_function({{my_function}}, ((76, 24, 11, 93, 28, 27, 95, 4, 39, 40, 11, 92),))
- # Tester reports function returned value 264, type int.
- # Tester reports test completed successfully.
- # </code>
- #
- # <code>
- # >>> test_function({{my_function}}, ((19, 88, 43, 15, 95, 89, 68, 72, 9),))
- # Tester reports function returned value 228, type int.
- # Tester reports test completed successfully.
- # </code>
- #
- # <code>
- # >>> test_interactive_task((0,35,3))
- # Enter the start value of the range: 0
- # Enter the stop value of the range: 35
- # Enter the step value of the range: 3
- # The sum of all even numbers from 0 to 35 by 3 is 90.
- # Tester reports test completed successfully.
- # </code>
- #
- # ------------------------------------------------------------------------------
- # Write a function that
- # takes in a list of numbers,
- # calculates the sum of all the EVEN numbers in the list
- # and returns the sum
- #
- # (see the "SHOULD WORK LIKE THIS" section for exact input and output format)
- #
- ###############################################
- # YOUR CODE GOES BETWEEN HERE... #
- # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv #
- def numList(x):
- summ = 0
- for i in x:
- if (i % 2) == 0:
- summ += i
- return summ
- # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #
- # ...AND HERE. #
- ###############################################
- if __name__ == '__main__':
- # these next lines do automatic testing for you:
- from TestMyCode import run_tests
- my_name = 'Keith' # put your name here so the tests can run.
- my_function = numList # put the name of the function you wrote so the tests can run.
- run_tests()
- # Ask the user for the start, stop and step values of a range.
- #
- # Call the function you wrote above
- # to calculate the sum of all the even numbers in the given range.
- #
- # Print out the calculated sum.
- #
- # (see the "SHOULD WORK LIKE THIS" section for exact input and output format)
- ###############################################
- # YOUR CODE GOES BETWEEN HERE... #
- # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv #
- num1 = int(input("Enter the start value of the range: "))
- num2 = int(input("Enter the stop value of the range: "))
- num3 = int(input("Enter the step value of the range: "))
- '''
- y = []
- for i in range(num1, num2, num3):
- y.append(i)
- '''
- y = range(num1, num2, num3)
- z = numList(y)
- print("The sum of all even numbers from " + str(num1) + " to " + str(num2) + " by " + str(num3) + " is " + str(z) + ".")
- # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #
- # ...AND HERE. #
- ###############################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement