coding at king ed’s (cake)  · web viewthe art of insult sword fighting21. the python project21....

45
CODING AT KING ED’S (CAKE) Simple python programming workbook MAY 18, 2020 KING EDWARD VI COLLEGE Computer Science

Upload: others

Post on 12-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Coding at King Ed’s (CAKE)

Computer Science Python Programming Workbook

Coding at King Ed’s (CAKE)Simple python programming workbook

ContentsIntroduction3Good Programming Practice41. Input and Output5Brief5Code5Explanation5Starter Tasks5Input and Output Exercises62. Basic Arithmetic8Brief8Code8Explanation8What if …….8Starter Tasks9Arithmetic Exercises103. Conditional Statements11Brief11Code11Explanation11Starter Tasks11An important note about conditions11Conditional Exercises12Example program12Conditional Statements Exercises134. Loops / Repeating Statements14Brief15Code15Explanation15Starter Tasks15Nested Loops (Loops within a Loop)16Brief16Code16Explanation16Starter Tasks16Loops Exercises175. Summer Exercises19Simple Exercises19Harder Exercises206. Classic Adventure Games – The Secret of Monkey Island Mini Project21The art of insult sword fighting21The Python Project21Bronze – The basics22Silver – Adding Single Player22Gold – Time to face the sword master22Not challenged enough?22Challenge 1 – Databases22Challenge 2 – Networks23Challenge 3 - Graphical23List of Insults and Responses23The Sword Master24Appendix - Solutions261. Input and Output Exercises262. Arithmetic Exercises283. Conditional Statements Exercises304. Loops Exercises32

Introduction

CAKE is a compilation of introductory explanations and exercises for python that should be tackled a bite (or should that be, byte) at a time. By completing it you’ll understand the four basic slices of programming (input and output, arithmetic operations, conditional statements and loops) and therefore be ready to start you’re A-level course.

It does not assume any knowledge of python programming, but to complete it you will need to visit python.org, download the latest version of python, and install it on your computer. Python comes with a simple editor called ‘Idle’ which is fine for these exercises. We suggest you start a separate new file for each exercise.

If you’ve done some python programming before (maybe at GCSE) you will still find many things in here that you did not know and exercises to challenge your programming skills.

At the end of each section (or slice) there are exercises to test your understanding. Answers to these are given in the appendix.

Once you’ve completed the booklet there are a series of Summer Exercises to tackle and a special mini project. For each of these you should copy and paste your finished code into a Word document (please include your name at the top of the document). When you start college in September we’ll ask you to submit this.

If you’re bitten by the programming bug and want to develop your python skills further we suggest you start here:

http://openbookproject.net/thinkcs/python/english3e/

https://pythonschool.net/category/basics.html

https://snakify.org/en/

Good Programming Practice

· Use white space to make the program easier to read. This means inserting blank lines between blocks of code so that it is obvious where one section ends and another begins.

· Use comments to explain what the program does and to identify the author, date written and filename.

· Choosing meaningful variable names helps the program to be self-documenting, that is, it helps other people to understand your code.

· camelCase makes it easier to read if you are going to use long compound words (words put together like firstNumber).

· Putting a space either side of operators such as ‘+’ or ‘/’ makes the operator stand out and improves program readability.

· Don’t use very long lines of code that continue all the way past the edge of the screen.

Programming code does not ‘wrap round’ as you type it in.

It’s much easier to read if it continues on the next line down.

In other words, don’t force the person reading your code to scroll right and left.

1. Input and OutputBrief

To ensure any computer program is useful we need to ensure that we can perform some basic tasks. The first of these is to output a message to the screen. Then the next step is to ask the user to input something and then output it back to the screen

Code

print("Hello World")

name = input("Please enter your name:- ")

print("Hello "+ name)

Explanation

In the code above we have a line which will just print out a simple “Hello World” to the screen this is usually the first program we would make to ensure that we can compile programs and run them correctly. Then we ask the user to enter their name and we then change our program to say “Hello ” so we end up with an interactive program.

‘name’ is a variable which we have created to store the result of the user input.

On the third line we use + which will join two strings together (Concatenation)

Starter Tasks

1. Ensure that you can get the very basic “Hello World” program running, put line 1 into the IDE and try to run the program. Save this as “Hello World.py”

2. Extend the sample program above so that line three says “Hello , how old are you?”

3. Get the user to input their age, store the result in a suitably named variable

4. Output the age of the user to the screen in a suitable sentence. The age should not be at the start or end of the sentence so you will need to join 3 strings together.

Input and Output Exercises

Exercise 1.1: Print out the following well-known song in exactly this format (keeping all the indentations and spaces between lines):

Twinkle, twinkle, little bat,

How I wonder what you're at!

Up above the world so high,

Like a tea tray in the sky.

Twinkle, twinkle, little bat,

How I wonder what you're at!

Hint: to do this efficiently look-up how to use tabs and newlines in python

Exercise 1.2: Ask the user to type in their first name as an input and display this back to them in the format “Your name is XXXX”

Exercise 1.3: Ask the user to enter their first name and last name separately and then their favourite colour. Display this information on the screen as “Your full name is XXXX XXXXX and your favourite colour is XXXX”.

Exercise 1.4: Ask the user for the radius of a circle, calculate the area of the circle and display it on the screen. Use 3.141592 as the value of π.

Exercise 1.5: Ask the user for two numbers and display on the screen the result of adding, subtracting, multiplying and dividing, e.g. if the numbers are 6 and 2, the output will be 8, 4, 12, 3.0.

Exercise 1.6: Ask the user for the length and width of a rectangle, calculate its area and display the result on the screen.

Exercise 1.7: Display on the screen your name and address (properly formatted as on an envelope), e.g.

Harry Potter

4 Privet Drive,

Little Whinging,

Surrey,

RU4 0WL

Continues next page

Exercise 1.8: Output a 25 X 8 rectangle on the screen. You can use any character.

Exercise 1.9: Output your name and address inside the 25 X 8 rectangle.

Exercise 1.10: Output a right-angled triangle on the screen as follows:

2. Basic ArithmeticBrief

Calculations are the core of most computer programs. They are designed to make life easier for people and one of the most time consuming things is people having to manually work out calculations by hand. Getting a computer to do it is so much faster.

Code

'''IntegerAdd - a program to add two integersLes TimmsSeptember 2017'''

firstNumber = int(input("Enter first integer: ")) #2 closing bracketssecondNumber = int(input("Enter second integer: "))

total = firstNumber + secondNumber

print ("The sum is:", total) # displays total

Explanation

The first part of the code between the triple quotes (''') is known as a “multiple line comment” the things written here will not be run but are there to allow a developer to leave some comments. In this case it is telling you who wrote the code and when it was written.

The bits of text after the # are known as a single line comment so anything after the # until the end of the line will not be run either. This is really useful for planning your code or writing notes to yourself to help you to understand what the code is doing

The function int is a special built in function which will take a string (one or more characters) and turn it into a whole number which can be used for calculations. If you just use input the python interpreter will assume that you want to use a string data type. You have to tell it explicitly that you want these characters to be treated as an integer data type.

What if …….

The user enters something other than an integer

This causes a run-time error to occur. In the example below the word ‘hello’ has been entered instead of an integer, and the message in the screenshot was the result. The important part is in red. Note it tells you the line on which the error has occurred (line 5) and that it is a “ValueError” because the value it has received is not within the normal range of integers (whole numbers). Later in the course we will learn how to trap and handle these errors.

Starter Tasks

1. Try printing out the result of 9 * 9 and then printing out the result of “9”*9 which should give you an idea of why we need to convert the number into an Integer. Incidentally, you can use this feature of python to easily insert a dividing line in your output.

For instance, try typing print (“_” *80) and see what happens

2. Check that you can do all of the basic maths operations by printing out an addition, multiplication, division and subtraction which all have the answer 10.

3. Try 5/2, 5//2 and 5%2. One of these gives you a decimal (fractional) Real number as an answer. One will give you the whole number part (Integer Division) and one will give you the remainder only (Modulus). Print 3 statements to the screen.

a. “The real number operator is ___ and gives the answer ___”

b. “The integer division operator is ___ and gives the answer ___”

c. “The modulus operator is ___ and gives the answer ___”

These two mathematical operators, integer division and modulus, are extremely useful in programming, and we will come across them again and again at A-level.

Arithmetic Exercises

In each of the programs below, try to follow the rules for good programming practice. Give each of your projects sensible names so that you will recognise them later, each exercise should be saved as a separate file. Save them in an appropriately named folder on OneDrive.

You do not need any techniques other than those covered in the course so far.

Exercise 2.1

Write a program which inputs two integers and outputs the result of multiplying the two integers together with sensible explanatory text

Exercise 2.2

Write a program which prompts the user for an integer, obtains it from the user and prints the square and cube of the number

Exercise 2.3

Write a program which prompts the user for two integers, obtains them from the user and prints their sum, difference, product, integer quotient and the remainder from the integer division

Exercise 2.4

Write a program to input two real numbers – use the data type float instead of int – and investigate the result of dividing different values using the ‘/’ and ‘//’ operators

Exercise 2.5

Modify the program you wrote for exercise 3 to use integers instead of real numbers. Is there any difference using real number division with integers?

Exercise 2.6

Write a program which prompts the user for three integers, obtains them from the user and prints their total and average

Exercise 2.7

Write a program which inputs from the user the radius of a circle and prints the circle’s diameter, circumference and area. Use the following formulae (r is the radius) and take to be 3.141592

Diameter = 2r Circumference = 2r Area = r2

3. Conditional StatementsBrief

Conditional Statements are key programming tools which allow us to make choices and do different things depending on what has happened. These can be a simple do A or B, or they could be a more complex list of If this do something, else if (shortened to elif in python) this do something different, else if this then do a third different thing.

Code

x = 5if(x > 6): print("X is bigger than 6")elif((x % 2) == 0): print("X is an even number")else: print("X is an odd number less than 7")

Explanation

In python, there is a colon after each statement (if, elif, else) and a TAB before print this tells the computer that the code is to be run if the condition is true. This is called a code block.

Starter Tasks

1. Ask the user to enter their age.

a. If they are older than 17 then output “You can go and vote!”

b. Else output “Sorry you will not be able to vote yet”

2. Now we need to adapt our program so that the rules are now

a. If they are less than 13 then output “You are not a teenager yet”

b. If they are less than 18 then output “You are a teenager, but not an adult yet”

c. Otherwise “You are an adult”

An important note about conditions

When describing logically what we want to do we might say something like “If X is 5 or 10 then do this…”Which people would often translate into if x == 5 or 10, which looks right and makes sense. However that statement will always evaluate to true.

Try running this code:x = 4if x == 5 or 10:print("x is either 5 or 10")

Notice how when you run it, the statement prints out, this is not what should happen. The reason for this is that python checks x is 5, and then it looks to see if 10 exists (which it always does). To get it to work we need to be explicit in our conditions.

x = 4if x == 5 or x == 10:print("x is either 5 or 10")

This will only print the statement if x is 5 or if x is 10.

Conditional Exercises

Python provides a facility for a program to make a decision based on the truth or falsity of some expression. This type of selection statement is an if statement. The if statement allows the program to select one direction or another to follow and it is therefore the whole process of using if, else if, and else statements is known as selection. The expression in the if statement is called a condition, and if the condition is true, the body of the statement executes. If the condition is not true, the body of the statement is not executed.

Conditions are formed by using the equality and relational operators shown in the table below. All these operators have the same order of precedence and are determined from left to right.

Algebraic operator

Python

operator

Example of Python condition

Meaning of Python condition

=

==

x == y

x is equal to y

or <>

!=

x != y

x is not equal to y

>

>

x > y

x is greater than y

<

<

x < y

x is less than y

>=

x >= y

x is greater than or equal to y

<=

x <= y

x is less than or equal to y

Example program

Notice that the first line of if statement must end with a colon (:) and the following lines that depend on it must be indented.

#Use two variables to store the values of the 2 integers

firstNum = int(input("Please enter the first integer: "))

secNum = int(input("Please enter the second integer: "))

if firstNum == secNum: #first number and second number are equal

print("Numbers are equal", firstNum, "=", secNum)

if firstNum != secNum: #first number and second number are not equal

print(firstNum, "not equal to", secNum)

if firstNum > secNum: #first number is greater than second

print(firstNum, ">", secNum)

Conditional Statements ExercisesExercise 3.1

Write a program to read an age from a user and output a message indicating whether the user is old enough to drive or not.

Exercise 3.2

Write a program which asks the user to input a number and which outputs a message “Positive”, “Negative” or “Zero” depending on the value of the number entered.

Exercise 3.3

Write a program that reads a first name and a second name from a user as two separate inputs, and concatenates the two names separating them by a space. Display the concatenated name on screen.

Exercise 3.4

Write a program to read a password input by a user and to compare it with one stored in the program. Output an appropriate message depending on whether the password is correct or not.

Exercise 3.5

Write a program that reads two integers and determines and prints whether the first is a multiple of the second. For example, if the user inputs 15 and 3 the first number is a multiple of the second. (Hint: Use Modulus)

Exercise 3.6

Write a program that takes as input the radius of a circle and calculates its area. If the area is less than 20 output the message “small circle area”, if between 20 and 40 “medium circle area, and if greater than 40 “large circle area”. This time import the ‘math’ module and use math.pi to dive you the value for pi.

Exercise 3.7

Write a program to read five separate numbers (integers) entered one at a time and determine and print the number of positive numbers input, the number of negative numbers input and the number of zeros input. Zero should not be counted as either a positive or negative number.

For example,

Input:

-3

67

-23

0

428

Output:

Positive numbers 2

Negative numbers 2

Zeros 1

4. Loops / Repeating Statements

If we want to repeat something is a program several times the best way to do this which avoids duplicating the code is to use a loop. Python only has two types of loop: the while loop and the for loop. The term for the use of these statements in a program is iteration. Let’s consider the differences between these types of loop.

The while loop is good for dealing with uncertainty. It is useful if you don’t know how many times you will need to repeat the code. In this example the program will keep asking for a number (integer) until the number 99 is entered. At that point the message ‘Loop finished’ appears on the screen and the program ends

number = 0

# We have to give an initial value to number before the loop starts

while number != 99: # remember != means ‘not equal to’

number = int(input('Please enter a number '))

print('Loop finished')

It’s used a lot in games where, for instance, you have to press the escape key to exit.

For loops are very useful when your program needs to repeat something a known number of times. There are two ways of writing for loops in python. The first is to specify the exact number of times you want the code to repeat. To do this you need to use the range function. Range counts from 0 so to repeat something 5 times you use range(1,6) or range (0,5). The example below prints the word ‘Message’ on the screen three times.

for i in range (0,3): #i simply means index number

print('Message')

The second type of for loop is not found in many other programming languages, but is extremely elegant and simple to use. It is used for iterating over a sequence. Later in the course we’ll learn about lists and tuples and how we can move through each item using a for loop. For now let’s stick to strings. Here is an example where we want to print all the letters in a string on a separate line. This works because a string is simply a list (or array, if we want to use the technical term) of characters

for letter in ‘pythonic’: #letter can be anything

print(letter)

The for loop is more controlled and more compact than the while loop, and you’ll encounter it a lot as we move onto more complex algorithms at A-level.

Brief

Loops are really useful programming constructs allowing us to repeat sections of code without having to re-type them. We can set up some conditions which will control when the loop ends, either after a certain number of steps, or when a Boolean flag is set.

Code

x = 0while x < 11: for i in range(1,11): print(str(x) + " multiplied by "+ str(i)+ " is "+ str(x * i)) #End for loop x = x + 1 #End while loop

Explanation

A while loop will continually run the contents so long as the condition evaluates to true ( while(True) will run forever and while(False) will never run). With while loops if you do not pay attention it is very easy to create an infinite loop.

Starter Tasks

1. Ask the user to enter the string and output to the screen any vowels found within the string.

2. Ask the user a question, while they respond with anything other than a “yes” keep repeating the question (annoyance game)

Nested Loops (Loops within a Loop)Brief

Nesting is when you have a loop within a loop. Often with the idea of enhancing efficiency within a program. You can have while loops in for loops and for loops in while loops and you can nest down to any level, you just need to account for how many overall loops will happen.

Code

for x in range(1,11):

for y in range(1,11):

print(str(x*y)+"\t",end='')

print("")

Explanation

By setting the range on the two loops the inner loop will go through a set of 10 repetitions then the outer loop will go through one loop forcing the inner loop to go through another set of 10.

When we print “\t” it tells the computer to print out a tab to help with spacingSetting end = “” tells the computer not to put a new line at the end of the printout.

A for loop will loop through a given set, that might be a range of numbers, a string (which is a set of characters) or a list (a set of sets) but the constraints to the loop are known at the start.

for [first iterating variable] in [outer loop]: # Outer loop

[do something] # Optional

for [second iterating variable] in [nested loop]: # Nested loop

[do something]

The program first encounters the outer loop, executing its first iteration. This first iteration triggers the inner, nested loop, which then runs to completion. Then the program returns back to the top of the outer loop, completing the second iteration and again triggering the nested loop. Again, the nested loop runs to completion, and the program returns back to the top of the outer loop until the sequence is complete or a break or other statement disrupts the process.

Remember, you can also loop through a string one letter at a timefor letter in string:

Starter Tasks

1. Loop through a string, one letter at a time and convert that letter into an integer based upon its ASCII code (hint:- ord)

2. I have encrypted the following string ‘mjqqt’ by shifting the letters in it by a fixed value, write a program which will try all 25 possible different shifts to decrypt the word.

a. Your program should output all 26 different combinations.

Loops ExercisesExercise 4.1

Write a program which will ask the user to enter a number. The program will then generate that number of Fibonacci numbers. If you want to know more about the Fibonacci sequence look it up on any Maths site.

Note: The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Exercise 4.2

Write a python program to read a number and print a right triangle using "*"E.g. : Input : 6----------Output---------

** ** * ** * * ** * * * ** * * * * *

Exercise 4.3

Now extend your triangle from the program above so that after it prints x stars, it prints a new row with 1 less until you reach 0.

E.g. :

Input : 6----------Output---------

** ** * ** * * ** * * * ** * * * * *

* * * * *

* * * *

* * *

* *

*

Exercise 4.4

Now turn your triangle into an arrow pointing to the left (instead of the right) E.g. :Input : 6----------Output---------

* * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Exercise 4.5

Write a program which will ask the user to enter a number. The program will then generate that number of Fibonacci numbers. If you want to know more about the Fibonacci sequence look it up on any Maths site.

Note: The first 10 Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

5. Summer Exercises

For each of these you should copy and paste your finished code into a Word document (please include your name at the top of the document). When you start college in September we’ll ask you to submit this.

Simple ExercisesExercise 5.1

Write a program that asks the user to input an integer other than zero and which responds with a message depending on whether the number is positive or negative.

Exercise 5.2

Write a program that determines whether a department store customer has exceeded the credit limit on their charge account. For each customer the user should input:

· The account number

· The balance at the beginning of the month

· The total of all items charged by the customer in the month

· The total of credits applied to the customer’s account in the month

· The allowed credit limit

The program should calculate the new end-of-month balance and determine whether it exceeds the customer’s credit limit. For customers whose limit is exceeded, the program should display the message “Credit Limit Exceeded”, otherwise any suitable message can be displayed

Exercise 5.3

Write a program that asks the user to input an integer other than zero and which responds with a message depending on whether the number is even or odd.

Exercise 5.4

Write two programs, one using a while loop, and one using a for loop that shows a countdown on the screen from 5 to 1 and then ends with the words “Blast off!”

5

4

3

2

1

Blast off!

To create a dramatic effect in your programs investigate how you might import and use the time module.

Harder Exercises

Exercise 5.5

A palindrome is a number or a text phrase that reads the same backwards as forwards, for example all the following five-digit integers are palindromes: 12321, 55555, 45554 and 11711. The five-digit integers 43235, 64445 and 12322 are not palindromes.

Write a program that reads in a five digit integer and determines whether it is a palindrome or not, printing a suitable message in each case.

Exercise 5.6

Using only the techniques learned so far, write a program which inputs an eight bit binary number and prints its decimal equivalent. You may need to read up on binary numbers first!

Exercise 5.7

Write a program to input a single character supplied by the user and output a message to say whether the character is in the first half (A – M) of the alphabet or the second.

Just be careful about upper and lower case characters – write your program for one or the other, but investigate what happens if you input a character in the wrong case.

Exercise 5.8

Write a program that inputs one number consisting of five digits from a user separates the number into its individual digits, and prints the digits separated from one another by three spaces each. For example, if the user types in the number 46382, the program should display

4 6 3 8 2

For this you should not convert the integers into a string! Think instead about using modulus and integer division.

6. Classic Adventure Games – The Secret of Monkey Island Mini Project

This is ‘mini project’ similar to a number you will do at A-level. It gives you a change to use the programming skills you have learned and produce something which will impress your friends and family.

There are three levels: bronze, silver and gold.

Everyone should be able to achieve at least the bronze level, but if you want to push yourself a bit further try silver, and if you feel that you are already a code-master and need stretching then go for gold and try some of the extra challenges!

One of my favourite games growing up was The Secret of Monkey Island (Lucasarts, 1990) it combines basic adventure game concepts and it also had an amusing combat system.

The art of insult sword fighting

The idea behind insult sword fighting is that each insult has a winning response, all other responses lose.

So for example if they insult is “You fight like a dairy farmer” then if I respond with “You run that fast” I would lose. If however I responded with “How appropriate, you fight like a cow” then I would win the round.

If you win 3 rounds then you win the match, otherwise you lose the match.

There is a nice web-based version showcasing the key element of how it works you can find at http://www.int33h.com/test/mi/. We will be focusing on the “Normal” mode from the Secret of Monkey Island.

The Python Project

I want you to simulate insult sword fighting, so that it can be played by 2 people at the same computer.

Bronze – The basics

To achieve a bronze level, you need to have the basic two player game working. For this I would suggest you have a list of the insults, and a list of the responses.

Allow player 1, to pick a number from 0 to the number of insults in your list, this number will tell you both the insult, and the position of the correct response.

Give player 2, four options of responses, one of which must be the correct response and 3 other random responses. Player 2 will chose a response and then the round will be won, or lost.

Player 2 will then pick a number and the above will repeat.

When one player has won 3 rounds, they will win the match. You win a round if you answer the insult correctly, or if your opponent doesn’t answer your insult correctly.

Silver – Adding Single Player

The original game was single player, and so now we need to bring in some non-player characters (NPCs).

For this modification it works closer to the original game, in that you start off with 2 insults, and you have to keep playing NPCs to learn more insults by losing fights.

You also have to learn the responses by seeing how NPCs respond to your insults, once you have seen a response you can then use it.

You will now “Win” this single player game when you have learnt all the insults from the list, and all the correct responses. This would be when you could challenge the sword master.

Assume the NPCs can, and will use any insult, and they will have a 50% chance of answering one of your insults correctly (hint: you will need the Random library)

Gold – Time to face the sword master

The sword master is a unique NPC that you can face only when you have collected enough insults and responses.

In the game, you can’t attack the sword master, you can only answer her insults correctly to win. However the sword master uses her own set of insults, but they have the same responses you have learnt, only you need to take the time to work out what the correct responses are.

You should implement the sword master, who has her own unique insults and instead of being able to fight back you can only defend.

To win you need to win 5 rounds instead of 3.

Not challenged enough?

So for those of you who want a bigger challenge there are a few extension ideas you could push yourself to look at. Note:- these are advanced techniques which would require you pushing yourself independently and are designed to allow you to explore different possible complexities and expand your skills.

Challenge 1 – Databases

Instead of using lists, you could explore how to interact with a database., Store all the insults and responses in a database table and use that table to play the game instead of lists.

Challenge 2 – Networks

Instead of having the basic 2 player game being played on the same machine, instead you could try to look at getting 2 computers talking to each other that way you could have networked multiplayer.

Challenge 3 - Graphical

You could make this work a little more like the website demo using something like Pygame to make a graphical game you could move about in and play.

List of Insults and Responses

Insult

Comeback

You fight like a Dairy Farmer!

How appropriate! You fight like a cow!

This is the END for you, you gutter crawling cur!

And I've got a little TIP for you, get the POINT?

I've spoken with apes more polite than you!

I'm glad to hear you attended your family reunion!

Soon you'll be wearing my sword like a shish kebab!

First you better stop waving it about like a feather duster.

People fall at my feet when they see me coming!

Even BEFORE they smell your breath?

I'm not going to take your insolence sitting down!

Your hemorroids are flaring up again eh?

I once owned a dog that was smarter than you.

He must have taught you everything you know.

Nobody's ever drawn blood from me and nobody ever will.

You run THAT fast?

Have you stopped wearing diapers yet?

Why? Did you want to borrow one?

There are no words for how disgusting you are.

Yes there are. You just never learned them.

You make me want to puke.

You make me think somebody already did.

My handkerchief will wipe up your blood!

So you got that job as janitor, after all.

I got this scar on my face during a mighty struggle!

I hope now you've learned to stop picking your nose.

I've heard you are a contemptible sneak.

Too bad no one's ever heard of YOU at all.

You're no match for my brains, you poor fool.

I'd be in real trouble if you ever used them.

You have the manners of a beggar.

I wanted to make sure you'd feel comfortable with me.

The Sword Master

Insult

Comeback

Now I know what filth and stupidity really are.

I'm glad to hear you attended your family reunion.

Every word you say to me is stupid.

I wanted to make sure you'd feel comfortable with me.

I've got a long, sharp lesson for you to learn today.

And I've got a little TIP for you. Get the POINT?

I will milk every drop of blood from your body!

How appropriate, you fight like a cow!

I've got the courage and skill of a master swordsman.

I'd be in real trouble if you ever used them.

My tongue is sharper than any sword

First, you'd better stop waving it like a feather-duster.

My name is feared in every dirty corner of this island!

So you got that job as a janitor, after all.

My wisest enemies run away at the first sight of me!

Even BEFORE they smell your breath?

Only once have I met such a coward!

He must have taught you everything you know.

If your brother's like you, better to marry a pig.

You make me think somebody already did.

No one will ever catch ME fighting as badly as you do.

You run THAT fast?

My last fight ended with my hands covered with blood.

I hope now you've learned to stop picking your nose.

I hope you have a boat ready for a quick escape.

Why, did you want to borrow one?

My sword is famous all over the Caribbean!

Too bad no one's ever heard of YOU at all.

You are a pain in the backside, sir!

Your hemorrhoids are flaring up again, eh?

I usually see people like you passed-out on tavern floor.

1. Even BEFORE they smell your breath?

2. I'm glad to hear you attended your family reunion.

There are no clever moves that can help you now.

Yes there are. You just never learned them.

Appendix - Solutions1. Input and Output Exercises

print('Exercise 1.1')

# \n newline. \t tabprint('''Twinkle, twinkle, little bat,\n\tHow I wonder what you're at! \n\t\tUp above the world so high,\n\t\tLike a tea tray in the sky.\nTwinkle, twinkle, little bat,\n\tHow I wonder what you're at!''')

print('Exercise 1.2')

name = input("What is your firstname? ")print("Your name is", name)

print('Exercise 1.3')

firstname = input("What is your firstname? ")lastname = input("What is your lastname? ")colour = input("What is your favourite colour? ")# \ indicates that there the code continues on the next lineprint ('Your full name is', firstname, lastname, \ 'and your favourite colour is', colour)

print('Exercise 1.4')

PI = 3.141592r = float(input ("Input the radius of the circle : "))print ("The area of the circle with radius", r, "is:",(PI * r**2))

print('Exercise 1.5')

firstnumber = int(input("Enter first number "))secondnumber = int(input("Enter second number "))

print("Firstnumber + Secondnumber =", firstnumber + secondnumber)print("Firstnumber - Secondnumber =", firstnumber - secondnumber)print("Firstnumber * Secondnumber =", firstnumber * secondnumber)print("Firstnumber / Secondnumber =", firstnumber / secondnumber)

print('Exercise 1.6')

length = float(input("Enter length "))width = float(input("Enter width "))

print("Area of rectangle =", length * width)

print('Exercise 1.7')print()print("Harry Potter")print()print("4 Privet Drive,")print("Little Whinging,")print("Surrey,")print("RU4 0WL")

print('Exercise 1.8')print()print("X"* 25)print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"* 25)

print('Exercise 1.9')print()print("X"* 25)print("X Harry Potter\t\tX")print("X 4 Privet Drive,\tX")print("X Little Whinging,\tX")print("X Surrey,\t\tX")print("X RU4 0WLX\t\tX")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"," "* 21, "X")print("X"* 25)

print('Exercise 1.10')print()print("X")print("X" * 2)print("X" * 3)print("X" * 4)print("X" * 5)print("X" * 6)print("X" * 7)print("X" * 8)print("X" * 9)

print('Exercise 1.10 version 2')

for i in range (10): #using a for loop print ("X" * i)

print ("-" * 50)

2. Arithmetic Exercises

print ('Exercise 2.1')

int1 = int(input("Enter first integer "))int2 = int(input("Enter second integer "))

print(int1, "multiplied by", int2, "=", int1 * int2)

print ('Exercise 2.2')

int1 = int(input("Enter integer "))

print("square =", int1**2, "cube =", int1**3)

print ('Exercise 2.3')

int1 = int(input("Enter first integer "))int2 = int(input("Enter second integer "))

print("sum =", int1 + int2)print("difference =", int1 - int2)print("product =", int1 * int2)print("integer quotient =", int1 // int2)print("remainder =", int1%int2)

print ('Exercise 2.4')

real_1 = float(input("Enter first real number "))real_2 = float(input("Enter second real number "))

print("division = ", real_1/real_2)print("floor division = ", real_1//real_2)

print ('Exercise 2.5')

int1 = int(input("Enter first integer "))int2 = int(input("Enter second integer "))

print("division = ", int1/int2)print("floor division = ", int1//int2)

print ('Exercise 2.6')

int1 = int(input("Enter first integer "))int2 = int(input("Enter second integer "))int3 = int(input("Enter third integer "))

total = int1+int2+int3average = total/3

print("total = ", total)print("average = ", average)

print ('Exercise 2.7')

PI = 3.141592 # Use of capitals denotes this is a constantradius = int(input("Enter the size of the radius "))diameter = radius * 2circum = (2 * PI) * radiusarea = PI * (radius **2)

print("diameter = ", diameter, " circumference = ", circum, "area = ", area)

3. Conditional Statements Exercises

print("Exercise 3.1")

age = int(input("Please enter your age "))if age < 17: print("You are not old enough to drive")if age >= 17: print("You are old enough to drive")

print("Exercise 3.2")

num = int(input("Enter number "))

if num > 0: print(num,"is positive")if num < 0: print(num,"is negative")if num == 0: print(num,"is zero")

print("Exercise 3.3")

firstname = input("Enter your firstname ")lastname = input ("Enter your lastname ")

print(firstname + " " + lastname)

print("Exercise 3.4")

password = input("Please enter password ")if password == "letmein": print("Password correct")else: print("Password not correct")

print("Exercise 3.5")

firstnumber = int(input("Enter first number "))secondnumber = int(input("Enter second number "))

if firstnumber % secondnumber == 0: print (firstnumber, "is a multiple of", secondnumber)else: print(firstnumber, "is not a multiple of", secondnumber)

print("Exercise 3.6")

import math

radius = int(input("Enter the size of the radius "))area = math.pi * (radius **2)if area < 20: print("small circle area")elif area <= 40: print("medium circle area")else: print("large circle area")

4. Loops Exercises

print("Exercise 4.1")

a = int(input("Enter the number of terms: "))first = 0 #first element of seriessec = 1 #second element of series

print("The requested series is:")if a == 0: print("not valid")elif a == 1: print(first)else: print(first,sec,end=" ") # end = " " prints on one line for x in range(2,a): next = first + sec print(next,end=" ") first = sec sec = next

print("Exercise 4.2")

n = int(input("Enter a number: "))

for i in range(0, n): print("*" * i)

print("Exercise 4.3")

n = int(input("Enter a number: "))

for i in range(0, n): print("* " * i)

for i in range(n, 0, -1): print("* " * i)

print("Exercise 4.3 alternative") #using nested for loops

n = int(input("Enter a number: "))

for i in range(n): for j in range(i): print ('* ', end="") print('')

for i in range(n, 0, -1): for j in range(i): print('* ', end="") print('')

print("Exercise 4.4") #This one takes some thinking about, but it uses the same techniques#as the previous two questions. See if you can solve it!

print("Exercise 4.5")

positive = 0negative = 0zero = 0for i in range (1,6): num = int(input("Enter number ")) if num > 0: positive +=1 elif num < 0: negative +=1 else: zero += 1print("Positive:", positive, "Negative:", negative, "Zero:", zero)