Spot the logic error

Below are 10 different functions. The syntax for these functions is correct, however when testing these functions, you will notice that they do not produce the expected output: each of these functions contain a logic error.

Did you know?

To troubleshoot a piece of code to spot logic errors is called debugging! The reason why a logic error is also called a bug is because one of the first computer programs to not behave as expected was due to an actual moth that was stuck in the electronic components of the computer creating a short circuit. Once the computer scientists spotted this bug, they removed it and the computer program worked again. Since then spotting and removing a logic error in a piece of code is called debugging the code!

Debugging Task

Your task is to copy the code of the following 10 functions in the online IDE provided below to then spot and fix the given code. Make sure to use a range of test data to fully test your functions.

Code #1Code #2Code #3Code #4Code #5Code #6Code #7Code #8Code #9Code #10

Our first function is used to find out if a number is odd or even. For instance check_end_or_odd(5) should output the word “Odd” on the screen.


def check_even_or_odd(number):
    if number % 2 == 1:
        print("Even")
    else:
        print("Odd")
        
check_even_or_odd(5)

Our second function is used to return the largest (max) of 3 numbers.


def findMax(a, b, c):
    if a > b or a > c:
        return a
    elif b > c:
        return b
    else:
        return c
        
print(findMax(7, 3, 9))

This function finds out if a given numebr is a prime number or not.


def isPrime(n):
    for i in range(2, n):
        if n % i == 0:
            return True
    return False

print(isPrime(5))

This function is used to reverse a string so for instance the string 101Computing should become gnitupmoC101.


def reverse_string(text):
    reversed_string = ""
    for char in text:
        reversed_string = reversed_string + char
    return reversed_string

print(reverse_string("101Computing"))

This function counts the number of vowels in a given string. So for instance in the string “Adele” we have 3 vowels.


def countVowels(name):
    vowels = "aeiou"
    count = 0
    for char in name:
        if char in vowels:
            count += 1
    return count

print(countVowels("Adele"))

This function claculate the iterative sum of a number e.gg Iterative Sum of 5 is 1 + 2 + 3 + 4 + 5 = 15


def iterativeSum(n):
    sum = 0
    for i in range(1,n):
        sum = sum + i
    return sum

print("iterativeSum(5) = 5 + 4 + 3 + 2 + 1 = " + str(iterativeSum(5)))

This function calculates a factorial value of any given number. For instance: %5 = 5 x 4 x 3 x 2 x 1 = 120


def factorial(n):
    if n == 0:
       return 1 #because 0! is 1
    else:
       f = 0
       for i in range(1,n+1):
          f = f * i
       return f

print("5! = 5 x 4 x 3 x 2 x 1 = " + str(factorial(5)))

This function counts the number of occurrences of a given value within a given list.


def count_occurrences(list, target):
    count = 0
    for element in list:
        if element == target:
            count = +1
    return count

print(count_occurrences([5, 2, 3, 5, 5, 4, 2], 5))

This function takes a list as a parameter and return another lists by removing all duplicate values from the initial list.


def remove_duplicates(list):
    unique_items = []
    for item in list:
        if item not in unique_items:
            unique_items.append(item)
    return list

print(remove_duplicates([1, 2, 2, 1, 3, 3, 4, 5]))

Our last function is used to display a tic tac toe (aka noughts and crosses) grid on the screen as a 3 by 3 grid.


def printGrid(grid):
   print(" |---+---+---|")
   line = ""   
   for i in range(0,3):
      for j in range(0,3):
         line = line + " | " + tictactoe[i][j]
      print(line + " |")
      print(" |---+---+---|")

tictactoe = [["X","O","X"],["O","X","O"],["X","X","O"]]
printGrid(tictactoe)

Online Python IDE

Use this online Python IDE to test the above functions, spot the logic errors and fix the code!

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 2.9 / 5. Vote count: 41

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!