0

can someone find the error

def safe_print_list(my_list=[], x=0): i = 0 count = 0 while (i < x): try: print("{:d}".format(my_list[i]), end="") count += 1 except (TypeError, ValueError, IndexError): pass i += 1 print("") return count my_list = [1, 2, 3, 4, 5] nb_print = safe_print_list(my_list, 2) print("nb_print: {:d}".format(nb_print)) nb_print = safe_print_list(my_list, len(my_list)) print("nb_print: {:d}".format(nb_print)) nb_print = safe_print_list(my_list, len(my_list) + 2) print("nb_print: {:d}".format(nb_print))

12th Oct 2024, 11:38 AM
Shadrack Muuo
1 Odpowiedź
+ 1
The code runs as written. The only issue I see is that on the exception, you are not printing anything so you don't see a message when there is an error. The error is being trapped, as it should, but you don't output any messages regarding the error. Try adding a print statements within your except block and see if that tells you what you need to know. except (TypeError, ValueError, IndexError): if TypeError: print(TypeError) if ValueError: print(ValueError) if IndexError: print(IndexError)
12th Oct 2024, 2:03 PM
Jerry Hobby
Jerry Hobby - avatar