Session 2: Homework

Create 3 variables to describe this module:

  1. the name (called name)
  2. the code (called code)
  3. the number of credits (called credits)

In [4]:
name = "Computational Thinking"
code = "CM6111"
credits = 20
print(credits)


20

In [5]:
from nose.tools import assert_equal

assert isinstance(name, str)
assert isinstance(code, str)
assert isinstance(credits, int)
assert_equal(credits, 20)
assert_equal(code, "CM6111")
assert_equal(name, "Computational Thinking")

Rewrite the above code to Multi-Variable assignment


In [ ]:


In [ ]:
assert_equal(credits, 20)
assert_equal(code, "CM6111")
assert_equal(name, "Computational Thinking")

Look at the code below and fix the string in order to ensure it can be printed.


In [7]:
my_string = "'Who's been eating my porridge?' said Papa Bear";
print(my_string)


'Who's been eating my porridge?' said Papa Bear

In [12]:
assert_equal(my_string, "Who\'s been eating my porridge? said Papa Bear")


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-12-feb9e1ec8738> in <module>()
----> 1 assert_equal(my_string, "Who\'s been eating my porridge? said Papa Bear")

/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py in assertEqual(self, first, second, msg)
    818         """
    819         assertion_func = self._getAssertEqualityFunc(first, second)
--> 820         assertion_func(first, second, msg=msg)
    821 
    822     def assertNotEqual(self, first, second, msg=None):

/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py in assertMultiLineEqual(self, first, second, msg)
   1191             diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
   1192             standardMsg = self._truncateMessage(standardMsg, diff)
-> 1193             self.fail(self._formatMessage(msg, standardMsg))
   1194 
   1195     def assertLess(self, a, b, msg=None):

/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py in fail(self, msg)
    663     def fail(self, msg=None):
    664         """Fail immediately, with the given message."""
--> 665         raise self.failureException(msg)
    666 
    667     def assertFalse(self, expr, msg=None):

AssertionError: "'Who's been eating my porridge?' said Papa Bear" != "Who's been eating my porridge? said Papa Bear"
- 'Who's been eating my porridge?' said Papa Bear
? -                              -
+ Who's been eating my porridge? said Papa Bear

Using operators, change the string below to print it 400 times.


In [16]:
to_print = "Me, but 400 more times"
print(to_print) #do not change or repeat this line


Me, but 400 more times

In [17]:
assert_equal(to_print, "Me, but 400 more times"*400)


---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-17-0f421c9fd298> in <module>()
----> 1 assert_equal(to_print, "Me, but 400 more times"*400)

/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py in assertEqual(self, first, second, msg)
    818         """
    819         assertion_func = self._getAssertEqualityFunc(first, second)
--> 820         assertion_func(first, second, msg=msg)
    821 
    822     def assertNotEqual(self, first, second, msg=None):

/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py in assertMultiLineEqual(self, first, second, msg)
   1191             diff = '\n' + ''.join(difflib.ndiff(firstlines, secondlines))
   1192             standardMsg = self._truncateMessage(standardMsg, diff)
-> 1193             self.fail(self._formatMessage(msg, standardMsg))
   1194 
   1195     def assertLess(self, a, b, msg=None):

/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/unittest/case.py in fail(self, msg)
    663     def fail(self, msg=None):
    664         """Fail immediately, with the given message."""
--> 665         raise self.failureException(msg)
    666 
    667     def assertFalse(self, expr, msg=None):

AssertionError: 'Me, [13 chars]times' != 'Me, [13 chars]timesMe, but 400 more timesMe, but 400 more ti[8733 chars]imes'
Diff is 8829 characters long. Set self.maxDiff to None to see it.

Use the variables below to assign the length of the string and convert the average word length to a string and assign the concatenated string of both variables to the final var


In [21]:
string_sample = "Hello my name is Gary and I love to write sentences with no punctuation and this makes it really deifficult to read and follow"
length_sample = #calc length here
string_avg = length_sample / len(string_sample.split(" "))
print(string_avg)
to_print = #print average word length AND string_sample here, like this <Average><String> (NO SPACES!)
print(to_print)


5.25

In [ ]:
assert_equal(string_avg, 5.25)
assert_equal(to_print, "5.25Hello my name is Gary and I love to write sentences with no punctuation and this makes it really deifficult to read and follow")

Why is the code below valid?

From your GCSE Maths knowledge, why are the 2 answers different?

What is the output of int(false)?


In [4]:
print(True + 3 * 100)
print((True + 3) * 100)


301
400

It is valid because the boolean type can be cast to an int value, allowing one to perform operations across types.

The brackets make up BODMAS, meaning that the order of operations changes based on the brackets. The first is: 3100 (300) + 1. The second is 1+3 (4) 100.

The output is 0