rosefun
rosefun

Reputation: 1857

How to continue running after correcting values when a bug occurs in ipython notebook debugging?

For example,

def power(x, n):
    res = 1
    for i in range(n):
        res *= x
    return res
power('10',5)

Then, it will raise the error as follows.

TypeError: can't multiply sequence by non-int of type 'str'

Now, I try to use %debug in the new notebook cell to debug.

ipdb> x = int(10)
ipdb> c

However, in %debug, if I use c which means continue in ipdb, it can't continue to run with changed value x.

So, I wonder if there is any method to correct the value of a variable while debugging and continue to run.


Update:

This is just an example.

In fact, I want to run the code for a long time in some cases, and an error occurs midway. I want to correct the error and try to continue to run the code. You know, simply re-running can take a long time.

Upvotes: 1

Views: 170

Answers (1)

Vaibhav Jadhav
Vaibhav Jadhav

Reputation: 2076

Check the below solutions:

x value should not be str type

Use power(10,5) instead of power('10',5)

OR

Convert value of x in the code:

def power(x, n):
    res = 1
    for i in range(n):
        res *= int(x)
    return res
print(power('10',5))

Upvotes: 1

Related Questions