Numbers Debugging#

section Title: umbers Debugging

  • Each of the following short code contains one or more bugs.     

  • Please identify and correct these bugs.

  • Provide an explanation for your answer.

Question-1#

x = 'five'
y = int(x)
print(y)

Solution

‘five’ cannot be converted to an integer. It should be given in digit form.

Question-2#

x = '5.07'
y = int(x)
print(y)

Solution

x is a string representation of a decimal number. It cannot be converted to an integer.

Question-3#

x = 5
print(10/(5-x))

Solution

Division by zero.

Question-4#

print(sum(4,7,9))

Solution

The numbers inside the sum() function can be enclosed in square brackets or parentheses.

Question-5#

print(sqrt(25))

Solution

sqrt() is not a built-in function; it needs to be imported.

Question-6#

print(math.sqrt(25))

Solution

The math module must be imported first.

Question-7#

x = '5'
y = 10
print(x+y)

Solution

Numbers and strings cannot be added or concatenated.

Question-8#

x = 1,234
y = 10
print(x+y)

Solution

In Python, underscores are used instead of commas for large numbers.

Question-9#

x = 3
y = 10
print(xy)

Solution

In Python, multiplication is performed using the * operator. For example, to multiply x and y, you would write x * y.