Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python-Functions-Assessment

The document outlines an assessment for Python functions, presenting a series of coding questions that require the creation and correction of various functions. Each question specifies the function's purpose, input parameters, and expected output, covering topics such as temperature conversion, summation, factorial calculation, and string manipulation. Additionally, a bonus question is included for further challenge, focusing on digit sums.

Uploaded by

eva acton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python-Functions-Assessment

The document outlines an assessment for Python functions, presenting a series of coding questions that require the creation and correction of various functions. Each question specifies the function's purpose, input parameters, and expected output, covering topics such as temperature conversion, summation, factorial calculation, and string manipulation. Additionally, a bonus question is included for further challenge, focusing on digit sums.

Uploaded by

eva acton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Functions Assessment

Blue = Given code

Example Question

def CtoF(temp_in_C):
temp_in_F = temp_in_C*1.8 + 32
return temp_in_F

Here is an example of a function.

This function converts temperatures in degrees Celsius to degrees Fahrenheit.

All of your solutions should have this format.

All you need to do for this example is submit!

Question 1

Write a function that converts temperatures in degrees Fahrenheit to temperatures in


degrees Celsius.

One version of the formula is C = (F - 32)/1.8

Your function should be named FtoC.


Your function should take a single input parameter (the temperature in degrees F).
Your function should return a single number (the temperature in degrees C).

Question 2

sum_between(start,end):
total = 0
for number in range(start,end):
total = total + number
return banana

Write a function called sum_between that finds the total of all numbers between two given
inputs.

For example
sum_between(1,10)
1+2+3+4+5+6+7+8+9+10 = 55

sum_between(20,25)
20+21+22+23+24+25 = 135

The function has been written, but it contains errors.


Fix the errors so that it runs with no errors and completes the calculations correctly.

Question 3

def factorial():
total = 0
for i in range(1,n+1):
total = total*i
return total

Write a function that calculates the factorial of any integer.

For example
5! = 5x4x3x2x1

The function should take a single input parameter and output the factorial of that number.

The function has been written, but contains some errors!


Fix the errors so that the function runs without errors and calculates factorials correctly.

Question 4

def positivity(numbers):

#missing line of code here

for num in numbers:


if numbers>0:
total=total+1
return total

Write a function positivity that counts the number of positive numbers in a list of numbers.

The function should take a single input parameter, which will be a list.
The function should return a single integer, which is count of positive numbers.
The function has been started, but there is a missing line of code.
There are also at least two further errors in the code.

Question 5

Write a function which counts the number of even and odd numbers in a list of numbers.

The function should be named odd_or_even

The function should take 1 input parameter - a list of positive ints.

The function should return a list containing 2 numbers. The number of odds should be first
and the number of evens should be second.

Question 6

Write a function that finds the average test score from a list of test scores.

The function should be named ave_test_score

The function should take 1 input parameter - a list of floats.

The function should return a single float.

Question 7

Write a function that takes a list of integers and double each number in the list.

The function should be names doubler.

The function should take 1 input parameter - a list of ints.

The function should return a list of ints.

Question 8

Write a function that counts the number of times that a particular character appears in a
string.

For example "i" appears in the string "I am going to be happy." 2 times.

NB You should count both upper-case and lower-case characters.


The function should be named character_counter.

The function should take 2 input parameters - first a string, second a single character.

The function should return a single int.

Question 9

Write a function which removes all of the vowels from a piece of writing.

Your function should be named the_Kiwi_translator.

Your function should take 1 input parameter - a string.

Your function should return a string.

Question 10

Write a function which adds up all of the digits in a given number.

For example the sum of the digits in 1234321 is 16.

Your function should be named digits_sum.

Your function should take 1 input parameter - a number.

Your function should return a single int.

Bonus Question

You should only attempt this if you have attempted all of the other problems.

Write a function which finds the digit sum of a number.

For example the digit sum of 1234321 is 7.


Because:
1+2+3+4+3+2+1 = 16
and
1+6 = 7

Name your function digit_sum.

You might also like