What's new

Help Lists three possibilities to consider if a function is not working

paulghette1026

Honorary Poster
Established
Joined
Aug 27, 2017
Posts
433
Reaction
110
Points
213
Section 6.9 Debugging of your textbook lists three possibilities to consider if a function is not working.



  • Describe each possibility in your own words.

  • Define "precondition" and "postcondition" as part of your description.

  • Create your own example of each possibility in Python code. List the code for each example, along with sample output from trying to run it.




The code and its output must be explained technically whenever asked. The explanation can be provided before or after the code, or in the form of code comments within the code. For any descriptive type of question, Your answer must be at least 150 words.

End your discussion post with one question related to programming fundamentals learned in this unit from which your colleagues can formulate a response or generate further discussion. Remember to post your initial response as early as possible, preferably by Sunday evening, to allow time for you and your classmates to have a discussion.

Your instructor will grade this assignment using You do not have permission to view the full content of this post. Log in or register now.

Reference​

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tea Press. You do not have permission to view the full content of this post. Log in or register now.
 
When a function is not working, there are three possibilities to consider:

1. Syntax Error: This occurs when there is a mistake in the code's structure or grammar, causing the code to be incorrect and potentially not run at all. A common example of a syntax error is a missing parenthesis or a typo in a variable name.

Example:
Python:
# Syntax Error
def add_numbers(x, y):
    result = x + y
    return result
Output: SyntaxError: invalid syntax

2. Runtime Error: This occurs when the code runs into a problem while executing, such as dividing by zero or accessing an index that is out of bounds. This can cause the code to crash or produce unexpected results.

Example:
Python:
# Runtime Error
def divide_numbers(x, y):
    result = x / y
    return result

divide_numbers(10, 0)
Output: ZeroDivisionError: division by zero

3. Logical Error: This occurs when the code runs without any errors, but it produces incorrect or unexpected results due to a flaw in the logic or algorithm. This is the most challenging type of error to detect and fix.

Example:
Python:
# Logical Error
def square_numbers(numbers):
    squared_numbers = []
    for num in numbers:
        squared_numbers.append(num * num)
    return squared_numbers

numbers = [1, 2, 3, 4, 5]
print(square_numbers(numbers))
Output: [1, 3, 6, 10, 15]

Precondition refers to the conditions that must be true before a function can be executed. It represents the requirements or constraints that must be satisfied.

Postcondition refers to the conditions that are guaranteed to be true after a function has been executed successfully. It represents the expected outcome or effects of the function.

Question: What strategies do you use to identify and fix logical errors in your code?
 
Back
Top