Commit 76e1005b authored by Toby Hodges's avatar Toby Hodges
Browse files

Merge branch 'karin' into 'master'

Karin



See merge request !5
parents 23945ed8 29ae5842
This diff is collapsed.
......@@ -119,11 +119,55 @@
#### _Exercise 2.2_
%% Cell type:markdown id: tags:
Change the program above to print out a message when a duplicate item is found. To do this, you could add another `if` statement to see if the item is in the list. Alternatively, you can add an `else:` clause to the existing `if` statement. This will be executed when the condition in the `if` statement is false.
(i) Change the program above to print out a message when a duplicate item is found. To do this, you could add another `if` statement to see if the item is in the list. Alternatively, you can add an `else:` clause to the existing `if` statement. This will be executed when the condition in the `if` statement is false.
(ii) The example illustrated above is not the only solution to adding items to a list, whilst checking for duplicates. From the three choices below, choose the version that would achieve the same goal:
%% Cell type:markdown id: tags:
a)
```
shopping = ['bread', 'potatoes', 'eggs', 'flour', 'rubber duck', 'pizza', 'milk']
extrashopping = ['cheese', 'flour', 'eggs', 'spaghetti', 'sausages', 'bread']
for item in extrashopping:
if item not in shopping:
print item, "is already in the list."
else:
shopping.append(item)
print(shopping)
```
%% Cell type:markdown id: tags:
b)
```
shopping = ['bread', 'potatoes', 'eggs', 'flour', 'rubber duck', 'pizza', 'milk']
extrashopping = ['cheese', 'flour', 'eggs', 'spaghetti', 'sausages', 'bread']
for item in extrashopping:
if item in shopping:
shopping.append(item)
else:
print item, "is already in the list."
print(shopping)
```
%% Cell type:markdown id: tags:
c)
```
shopping = ['bread', 'potatoes', 'eggs', 'flour', 'rubber duck', 'pizza', 'milk']
extrashopping = ['cheese', 'flour', 'eggs', 'spaghetti', 'sausages', 'bread']
for item in extrashopping:
if item in shopping:
print item, "is already in the list."
else:
shopping.append(item)
print(shopping)
```
%% Cell type:markdown id: tags:
#### Counting Loops
......@@ -292,11 +336,21 @@
#### _Exercise 2.5_
%% Cell type:markdown id: tags:
Change your program to print out a formatted message for each of the items in your shopping list along with the amount you need to buy of that item.
In the example below, we have changed the program so it prints out a formatted message for each of the items in the shopping list along with the amount that needs to be bought of that item. Parts of the program are missing. You need to fill them in:
%% Cell type:code id: tags:
``` python
shopping = ['bread', 'potatoes', 'eggs', 'flour', 'rubber duck', 'pizza', 'milk']
amounts = [1, 10, 12, 1, 2, 5, 1]
--- i in range(len(---)):
s = 'I need to buy --- ---'.format(amounts[---], ---[i])
print ---
```
%% Cell type:markdown id: tags:
#### Looking Up Data
......@@ -497,19 +551,27 @@
#### _Exercise 2.7_
%% Cell type:markdown id: tags:
In worksheet one, exercise 1.1, you should have worked out an expression for calculating the hypotenuse of a right angled triangle given the other two sides. Now, create a function called `hypot()` to do this, so that you could call
In worksheet one, exercise 1.1, you should have worked out an expression for calculating the hypotenuse of a right angled triangle given the other two sides. Now, complete function below, which should calculate the hypotenuse, and test it by calling
%% Cell type:markdown id: tags:
`hypot(3,4)`
%% Cell type:markdown id: tags:
and it would return the value 5.
and it should return the value 5.
%% Cell type:code id: tags:
``` python
--- hypot(---, sideb)---
h = ---.sqrt(sidea**--- --- ---**2)
return ---
```
%% Cell type:markdown id: tags:
Once you have a function that you think works, try using `math.hypot()` to check your answers.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment