The code on the left defines a function score() that takes no arguments, but defines the global variable score throughout the program. Modify the code on the left so that the output of your program is 0. In fact, if you try to run the following command python Q1.py in the Terminal window, you see that the output produced is not the one that you would expect.
Below, you have been provided with a simple debugging tool: Code Visualizer. Click on the button Code Visualizer to launch it and debug your code. Feel free to resize the windows to see the complete output.
HINT: You must indent the blocks below. You can do so by dragging them.
def score():
global score
score = 0
print(score)
score()
The code on the left defines a function add_underscores()
that
takes a single string object word as an argument and
returns a new string containing a copy of the word with
each character surrounded by underscores.
For example, add_underscores("python")
should return _p_y_t_h_o_n_
.
However, if you try to run the following command python Q2.py in the Terminal window, you see that the output produced is not the one that you would expect.
Below, you have been provided with a simple debugging tool: Code Visualizer. Click on the button Code Visualizer to launch it and debug your code. Feel free to resize the windows to see the complete output.
def add_underscores(word):
new_word = "_"
for i in range(len(word)):
new_word =new_word+ word[i] + "_"
return new_word
word = "python"
print(add_underscores(word)
The code on the left contains the function average that takes two numbers as input and returns their average.
However, if you try to run the following command python Q3.py in the Terminal window, you will see that the code has bugs and the output produced is not the one that you would expect.
Below, you have been provided with a simple debugging Tool: Code Visualizer. Click on the button Code Visualizer to launch it and debug your code. Feel free to resize the windows to see the complete output.
def average(x, y):
result = x+y/2
return result
print(average(4.0, 9.0)) # should print 6.5
def average(x, y):
result = 0
result += x
result += y
result /= 2
return result
print(average(4.0, 9.0)) # should print 6.5
The function buggy_function() on the left has exactly one bug.
The function should return True
if
the input x
is either an even integer less than or equal to 10,
or an odd integer greater than 10.
Otherwise, the function should return False.
However,
when you run the function in the Terminal window via python Q4.py
with the argument 14,
you see an unexpected result.
Below, you have been provided with a simple debugging Tool: Code Visualizer. Click on the button Code Visualizer to launch it and debug your code. Feel free to resize the windows to see the complete output.
def buggy_function(x):
is_even = (x % 2 == 0)
if type(x) != int:
return False
if x <= 10:
if is_even:
return True
if x > 10:
if not is_even:
return True
else:
return False
test = buggy_function(14)
print(test)
def buggy_function(x):
if type(x) != int:
return False
if x > 10 and x % 2:
return True
if x <= 10 and not x % 2:
return True
return False
print(f'Test x = 14: {buggy_function(14)}')
print(f'Test x = 13: {buggy_function(13)}')
print(f'Test x = 9: {buggy_function(9)}')
print(f'Test x = 8: {buggy_function(8)}')
print(f'Test x = 3.5: {buggy_function(3.5)}')
The function factorial()
on the left computes the factorial of
a number via the formula:
$$n! = n \times (n - 1) \times (n − 2) \times \dots \times 1$$
However, when you try to run the script in
the terminal via python Q5.py
you will see that,
due to the presence of bugs, the code outputs a wrong result.
Below, you have been provided with a simple debugging Tool: Code Visualizer. Click on the button Code Visualizer to launch it and debug your code. Feel free to resize the windows to see the complete output.
def factorial(n):
counter = 0
total = n
while counter <= n:
total *= (n-counter)
counter += 1
return total
print(factorial(3)) #should print 6
def factorial(n):
counter = 1
total = 1
while counter <= n:
total *= (counter)
counter += 1
return total
print(factorial(3)) #should print 6
Have you debugged any programs before? From what you have learned in this module so far, what is the purpose of debugging, and how do you feel that it may be useful to you in future work? What would be a good strategy to find bugs quickly in your program using the debugging process?
In your post, answer the questions above and include an example of Python code that contains at least two syntax errors. Explain which steps you would consider taking to debug your code and fix the errors.
Discussion Prompt:
Read the statements posted by your peers. Engage with them by responding with thoughtful comments and questions to deepen the discussion.
Suggested Time: 45 minutes
Suggested Length: 250 words
This is a required activity and will count toward course completion.
In this activity,
you will be required to download the Jupyter Notebook file MIT.ipynb
on
your local machine and demonstrate your understanding of GitHub and its commands.
To complete the activity, perform the following steps:
PCDE-Activity 9.1
.MIT.ipynb
file to
remove any errors.
There are three obvious mistakes in the file.
Make sure you fix them all.
Add any additional file you may need to your repository to ensure that
the content of the Notebook renders as expected.README.md
file in
your remote repository and try to edit it so
it displays Repository for Activity 9.1
as a Markdown header.