Fixing some examples of broken code

Posted on 27-02-18 in Exercises

How to fix code

In this article we look at some very short examples of code and the errors that Python will state. The aim is for the reader to fix each bit of code.

To identify the kind of error present the first thing we do is read the error message, if there is one. Some errors aren't so clear. In these cases read the code carefully.

I would recommend typing the code into your editor of choice and seeing if you can correct the mistake!

Have a tab of https://learnxinyminutes.com/docs/python3/ open to check your syntax carefully.

1 - Print statements

In [1]:
print "Hello"
  File "<ipython-input-1-dabf489e9f92>", line 1
    print "Hello"
                ^
SyntaxError: Missing parentheses in call to 'print'

Expected Output : "Hello"

In [2]:
print("hello"
  File "<ipython-input-2-075bb1d15818>", line 1
    print("hello"
                 ^
SyntaxError: unexpected EOF while parsing

Expected Output : "Hello"

In [3]:
print(hello)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-1cd80308eb4c> in <module>()
----> 1 print(hello)

NameError: name 'hello' is not defined

Expected Output : "Hello"

In [4]:
Variable = "hello"
print(variable)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-64346e22e1d9> in <module>()
      1 Variable = "hello"
----> 2 print(variable)

NameError: name 'variable' is not defined

Expected Output : "Hello"

In [5]:
some_number = 5
print("some_number")
some_number

Expected Output : 5

In [6]:
some_number = '5'
print(some_number + 2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-6dc49be68f6f> in <module>()
      1 some_number = '5'
----> 2 print(some_number + 2)

TypeError: Can't convert 'int' object to str implicitly

Expected Output : 7

In [7]:
first = 20
second = 5
print(first, "+", second, "is", first*second)
20 + 5 is 100

Expected Output : 25

2 - Type Errors

In [1]:
a = '5'
a + 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-2805cd7f3c14> in <module>()
      1 a = '5'
----> 2 a + 2

TypeError: must be str, not int

Expected Output : 7

In [2]:
a = 5
'a' + 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-34c695320918> in <module>()
      1 a = 5
----> 2 'a' + 2

TypeError: must be str, not int

Expected Output : 7

In [3]:
fruits = ['apples', 'pears', 'oranges']
fruits + 'strawberries'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-6388e93538c4> in <module>()
      1 fruits = ['apples', 'pears', 'oranges']
----> 2 fruits + 'strawberries'

TypeError: can only concatenate list (not "str") to list

Expected Output : ['apples', 'pears', 'oranges', 'strawberries']

In [4]:
12 // '2'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-d3407c815911> in <module>()
----> 1 12 // '2'

TypeError: unsupported operand type(s) for //: 'int' and 'str'

Expected Output : 6

In [5]:
'new york ' ** 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-15d8496353c8> in <module>()
----> 1 'new york ' ** 2

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

Expected Output : 'new york new york '

3 - Looping statement

In [1]:
for x in range(3)
    print(x)
  File "<ipython-input-1-8073f938f1ad>", line 1
    for x in range(3)
                     ^
SyntaxError: invalid syntax

Expected Output :

0
1
2
In [2]:
for j in range(5):
    print("This is loop number j")
This is loop number j
This is loop number j
This is loop number j
This is loop number j
This is loop number j

Expected Output :

This is loop number 0
This is loop number 1
This is loop number 2
This is loop number 3
This is loop number 4
In [3]:
while x > 0:
    print(x)
    x -= 1
    
x = 10
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-bff16e6dec6e> in <module>()
----> 1 while x > 0:
      2     print(x)
      3     x -= 1
      4 
      5 x = 10

NameError: name 'x' is not defined

Expected Output :

10
9
8
7
6
5
4
3
2
1
In [4]:
countdown = 0
while countdown:
    print("{}!".format(countdown))
    countdown -= 1
else:
    print("Lift off!")
Lift off!

Expected Output :

5!
4!
3!
2!
1!
Lift off!