0% found this document useful (0 votes)
20 views

Computer Science Tasks

The document describes a Python module project with multiple tasks involving strings, functions, conditionals and control flow. Students are to write code to complete the tasks, testing it online before submitting. The tasks include string slicing, concatenation, defining functions to check string length and return grades based on percentages.

Uploaded by

ProffessorDL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Computer Science Tasks

The document describes a Python module project with multiple tasks involving strings, functions, conditionals and control flow. Students are to write code to complete the tasks, testing it online before submitting. The tasks include string slicing, concatenation, defining functions to check string length and return grades based on percentages.

Uploaded by

ProffessorDL
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TY Python Module 2

Syntax / Strings / Conditionals and Control Flow / Functions


MODULE PROJECT

Instructions

This is the module project and is worth 50% of the module mark.

This project covers the the four topics covered during the module:

 Syntax
 Strings
 Conditionals and Control Flow
 Functions

The project gives each student an opportunity to put into practice what they
have learned about programming in Python by problem-solving and completing a
varied set of tasks.

There are various tasks described below.

Write code for each task using the Programiz online Python compiler available
at:

https://www.programiz.com/python-programming/online-compiler/

You can check your code by running it in the compiler.

When you are happy with your code, paste it into the space provided at the very
end of this Word document. Then, attach the completed Word document to the
PROJECT assignment n Assignments and click on HAND-IN.

The deadline for completion is Thursday 24th November at 9 PM.

You can use the class notes (videos and PowerPoints), the Codecademy lessons,
and the internet to help you complete the tasks.

GOOD LUCK!!
Task 1 (20 marks)

Create a string called new_string.


This string should contain the value “We don’t need no ”.
[6 marks]

Write code which prints the first, second and sixth elements of this string.
Hint: Google slicing in Python.
[3 marks]

Create a string called new_string2.


This string should contain the value “education”.
[2 marks]

Run the following line of code:


print(new_string + new_string2)
[3 marks]

Do an internet search for the next line of lyrics in the song whose first line of
lyrics is the statement outputted by running the line of code above.

Create a new string called new_string3 which contains the first two lines of
lyrics in this song.

Call the print command on new_string3.


[2 marks]

Use the len function and print command to print the length of new_string3.
[2 marks]

Using correcting indexing, convert all characters in new_string3 to lowercase


form, apart from the words ‘education’ and ‘thought control’. Convert these
words to uppercase form.

Store this new lowercase/uppercase version of the song lyrics in a new variable
called new_string4.

Hint: Do an internet search for upper() and lower() methods for strings in
Python.
[1 mark]

Print the value of new_string4.


[1 mark]
Task 2 (5 marks)

Write code to create a function called my_fn_1 which takes as a first argument
a string, and takes as a second argument a numerical value.

If the length of the inputted string exceeds this numerical value, the function
should return a statement which reads:

“The input is too long as it exceeds the maximum permitted value of x.”

x should be the numerical value inputted as the second argument.

If the length of the inputted string is less than or equal to the numerical value
(second argument), the function should return a statement which reads:

“The input length is within permitted limits. There are y characters remaining.”

y should be the non-negative difference between the permitted maximum length


and the length of the inputted string.

Having defined my_fn_1, run the code below to check that your code works
correctly:

print(my_fn_1(“I think this string is too long”, 10))


print(my_fn_1(“I think this string is fine”, 30))
[5 marks]

Task 3 (5 marks)

Write code to create a function called my_fn_2 which takes as an argument a


whole number percentage mark from a test and returns a grade.

The grading system to be used is: Distinction (70-100), Upper Merit (60-69),
Merit (50-59), Pass (40-49), Not Achieved (0-39).

This function should return the correct grade for any whole number percentage
mark entered.

Having defined my_fn_2, run the code below to check that your code works
correctly:

print(my_fn_2(92))
print(my_fn_2(66))
print(my_fn_2(50))
print(my_fn_2(43))
print(my_fn_2(10))
[5 marks]

Space for your code

Copy and paste your code into the spaces provided below.

Paste into the correct section.

Make sure your code pastes exactly as you have written.

Marks will be only be awarded for code which runs correctly.

Spelling errors, typos, formatting errors, etc. will cause your code not to run
correctly.

Task 1

new_string = "We don't need no "


print (new_string[0] + new_string[1] + new_string[5])

new_string2 = "education"
print (new_string + new_string2)

new_string3 = """We don't need no education, We don't need no thought


control"""
print (new_string3)
print (len(new_string3))

new_string4 = new_string3[0:17].lower() + new_string3[17:26].upper() +


new_string3[26:44].lower() + new_string3[44:60].upper()
print (new_string4)

Task 2

def my_fn_1(a_string,x):
y = abs((int(x) - len(a_string)))

if len(a_string) > x:
return ("The input is too long as it exceeds the maximum permitted value
of " + str(x))

else:
return ("The input length is within permitted limits. There are ") + str(y) +
(" characters remaining.")
print(my_fn_1("I think this string is too long", 10))
print(my_fn_1("I think this string is fine", 30))

Task 3

def my_fn_2(n):
if n > 69:
return ("Distinction")
elif n > 59:
return ("Upper Merit")
elif n > 49:
return ("Merit")
elif n > 39:
return ("Pass")
else:
return ("Not Achieved")

print(my_fn_2(92))
print(my_fn_2(66))
print(my_fn_2(50))
print(my_fn_2(43))
print(my_fn_2(10))

You might also like