Exercise 1: Syntax errors
Contents
Exercise 1: Syntax errors#
In this, and subsequent, noteboooks in this class we’ll walk through a number of different problems. Each of these fail in some way. For many of the early problems, the issue can be solved by inspecting the code. However, for later ones you may need to use PDB to really understand what the code is doing and diagnose the problems. Some of the later problem sets are quite challenging, so just get through as much of this as you can. There are no “summary exercises” this week, just work through as much as you can. Try to get at least as far as exercise 5. Good luck!
Syntax errors#
Syntax errors are the first type of bug you encounter in new code. Luckily these prevent the code from running so you know there’s a problem, you just need to fix it.
Here are some examples of code containing syntax errors. Identify and fix the issues in each of the cells below. In some cases there are errors beyond just the syntax error to resolve!
Exercise 1.1#
counter=0
While counter < 5:
print ("hello")
counter = counter + 1
Input In [1]
While counter < 5:
^
SyntaxError: invalid syntax
Exercise 1.2#
counter = 0
if counter == 4
print ("counter is 4")
Input In [2]
if counter == 4
^
SyntaxError: invalid syntax
Exercise 1.3#
x = 3
y = ((x + 3) * (x + 4)
print y
Input In [3]
print y
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(y)?
Exercise 1.4#
name = input)
print(name)
Input In [4]
name = input)
^
SyntaxError: unmatched ')'
Exercise 1.5#
greeting = "hello" + " world
print(greeting)
Input In [5]
greeting = "hello" + " world
^
SyntaxError: EOL while scanning string literal
Exercise 1.6#
def example1_A()
x = 0
y = 1
return x+y
print(example1_A())
Input In [6]
def example1_A()
^
SyntaxError: invalid syntax
Exercise 1.7#
import numpy
def example1_B(list_of_vals):
b = ((list_of_vals.sum() + list_of_vals.mean() + numpy.cos(list_of_vals)*(sorted(list_of_vals)))
return b
a = numpy.random.random(100)
example1_B(a)
Input In [7]
return b
^
SyntaxError: invalid syntax
Exercise 1.8#
first = 1
second = 2
third =
extra = input("What is the extra value? ")
total = first+second+third+extra
print(total)
Input In [8]
third =
^
SyntaxError: invalid syntax
Exercise 1.9#
For this one here, after fixing, ensure it works with both an odd and an even number as input!
x = int(input('Enter a number: '))
whille x%2 == 0:
print('You have entered an even number.')
else:
print ('You have entered an odd number.')
Input In [9]
whille x%2 == 0:
^
SyntaxError: invalid syntax
Exercise 1.10#
The following example case is long. It’s an example of coding up a noughts and crosses game. There’s nothing in here that you haven’t seen before, so can you use the error messages, and your knowledge, to fix the broken code? (If you get stuck, don’t spend too much time on this, it’s important to also see later exercises!)
HINT: After resolving the syntax errors, are the input, and output, arguments in the right order??
import numpy
def create_empty_board()
return numpy.zeros([3,3)
def print_board(board):
for i in range(3):
curr_line = ''
for j in range(3):
if board[i,j] == -1:
curr_line += "O"
elif board[i,j] == 1:
curr_line += "X"
elif board[i,j] == 0:
curr_line += "*"
print(curr_line)
print()
def place_nought(board, current_player, i, j):
if current_player == 'crosses':
print("It's not nought's turn!")
return board, current_player
if not board[i,j] == 0:
print("That square already has a piece on it")
return board, current_player
board[i,j] = -1
current_player = 'crosses'
check_board_status(board)
return board, current_player
def place_cross(board, current_player, i, j):
if current_plazer == 'noughts':
print("It's not nought's turn!")
return board, current_player
if not board[i,j] == 0:
print("That square already has a piece on it")
return board, current_player
board[i,j] = 1
current_player = 'noughts
check_board_status(board)
return board, current_player
def check_board_status(board):
# Check horizontal and vertical rows
diag_sum = board[0,0] + board[1,1] + board2,2]
diag_sum2 = board[0,2] + board[1,1] + board[2,0]
for i in range(3):
if board[i,:].sum() == 3 or board[:,i].sum() == 3 or diag_sum == 3 or diag_sum == 3:
print("CROSSES WIN")
print_board(board)
if board[i,:].sum() == -3 or board[:,i].sum() == -3 or diag_sum == -3 or diag_sum == -3:
print("NOUGHTS WIN")
print_board(board)
# Check if any values are 0
if not (board == 0).any():
print("GAME IS A DRAW")
print_board(board)
board = create_empty_board()
current_player = 'noughts'
print_bord(board)
current_player, board = place_nought(current_player, board, 0,1)
# This one below should fail, it's not nought's turn!
current_player, board = place_nought(current_player, board, 1,1)
print_board(board)
current_player, board = place_cross(current_player, board, 1,1)
current_player, board = place_nought(current_player, board, 0,0)
current_player, board = place_cross(current_player, board, 2,2)
current_player, board = place_nought(current_player, board, 0,2)
# Nought wins
board = create_empty_board()
current_player = 'noughts'
print_bord(board)
current_player, board = place_nought(current_player, board, 1,1)
current_player, board = place_cross(current_player, board, 0,0)
current_player, board = place_nought(current_player, board, 0,2)
current_player, board = place_cross(current_player, board, 2,0)
current_player, board = place_nought(current_player, board, 1,0)
current_player, board = place_cross(current_player, board, 1,2)
current_player, board = place_nought(current_player, board, 0,1)
current_player, board = place_cross(current_player, board, 2,1)
current_player, board = place_nought(current_player, board, 2,2)
# DRAW
Input In [10]
def create_empty_board()
^
SyntaxError: invalid syntax