Last answered:

03 May 2022

Posted on:

01 May 2022

1

Why the code execute successfully but the value of function does not show on the result?

For example, in the video

print("Entering try block")
try:
     division2(3.0,2.0)
except ZeroDivisionError as err:
     print(err)
     print("Cannot divide by zero")
except TypeError as t_err:
     print(t_err)
     print("Please input a number")
print("Program finished")

and the output are only
Entering try block
Let's get started
All done!
Program finished

why the value of 3.0/2.0 does not show on the output

1 answers ( 0 marked as helpful)
Instructor
Posted on:

03 May 2022

0

Hey I-Cheng,

Thank you for your question!

The way the division2() function is defined is such that it only calculates the division and returns the answer. The function doesn't print it out the result. To have it displayed on the screen, modify the code as follows:

print("Entering try block")
try:
     print(division2(3.0,2.0))
except ZeroDivisionError as err:
     print(err)
     print("Cannot divide by zero")
except TypeError as t_err:
     print(t_err)
     print("Please input a number")
print("Program finished") 

Here, I have put division2(3.0,2.0) inside a print() function.

Keep up the good work!

Kind regards,
365 Hristina

Submit an answer