8) a. Traceback Examples




Xena is a final year computer science student and is trying to create a software that gives ‘internet points’ to some random user on her website. But she has forgotten some really important piece of code and the functionality is breaking because of it. Can you figure out the failing piece of code and fix the code for her. The code and error is given to you for the same.

1
2
3
4
5
6
7
8
9
# traceback:
Traceback (most recent call last):
  File "internet_points.py", line 13, in <module>
    main()
  File "internet_points.py", line 10, in main
    give_internet_points(total_points, rand_num)
  File "internet_points.py", line 5, in give_internet_points
    points_per_user = total_points / num_of_users
ZeroDivisionError: division by zero




Ripple wants to give a demo in his home on python files and wants to impress his friends but his code is giving IOError. You are his friend and wants to help him fix some minor bugs in his code. Please help him fix the issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Program:
#file_handler.py

def read_file():
    with open('demo_file.txt', 'r') as file_read:
        print(file_read)

#demo_code.py
import file_handler
def main():
    print('Reading a demo file')
    file_handler.read_file()
   
if __name__ == '__main__':
    main()
1
2
3
4
5
6
7
8
9
10
#traceback
Reading a demo file
Traceback (most recent call last):
  File "demo_code.py", line 7, in <module>
    main()
  File "demo_code.py", line 4, in main
    file_handler.read_file()
  File "E:\demo_files\file_handler.py", line 2, in read_file
    with open('demo_file.txt', 'r') as file_read:
FileNotFoundError: [Errno 2] No such file or directory: 'demo_file.txt'




Musa is a bird watcher and updates a list of birds that he has seen in a python list. Now, after 13 years of watching birds he wants to see all the birds that he has seen. He has written a script that does the same but it is failing and giving him an error. He is running the same code on a smaller list but the code is still failing. Please help him fix the issue so that he can continue watching the birds.

1
2
3
4
5
6
7
8
9
10
#traceback
The bird watched is Rainbow lorikeet
The bird watched is Monk parakeet
The bird watched is Sumatran Ground-cuckoo
The bird watched is Egyptian Vulture
The bird watched is Bahia Tapaculo
Traceback (most recent call last):
  File "bird_watching.py", line 5, in <module>
    print('The bird watched is {}'.format(list_of_birds[index]))
IndexError: list index out of range




Kinu is a store manager for General Stores Ltd. in Neverland. He has a program with him that gets him the number of items in stock for any item entered but the program fails on specific inputs. Xinu hires you to fix the issue and return him with 0 in case the item is not present in stock. You want to fix this issue and go back to watching cat videos. The traceback is given below. Please help him fix this.

1
2
3
4
5
6
7
#traceback
Enter the item for which you want to find the stock for
Paper Pins
Traceback (most recent call last):
  File "store_stocks.py", line 5, in <module>
    print('The item is {} and the no of items in stock is {}'.format(item, stock[item]))
KeyError: 'paper pins'




Debbie is a magician who has the power to calculate square of any number within seconds. Debbie is also a python expert and has created a script for the same. Whenever a number is given to him, he puts it into hisprogram and the result is displayed on the screen. But some people have started to give Debbie strings due to which Debbie’s code is failing. Debbie has a lot of magical things to do and does not have time to fix this issue. Please help him filter out the string inputs by looking at the traceback.

1
2
3
4
5
6
7
8
9
10
11
12
13
Program:
#magic_square.py

def square(num):
    return num * num
   
def main():
    print('Enter the number whose square is needed')
    number = int(input()) #Enter
    print('The square is {}'.format(square(number)))

if __name__=='__main__':
    main()
1
2
3
4
5
6
7
8
9
#traceback
Enter the number whose square is needed
fifteen
Traceback (most recent call last):
  File "magic_square.py", line 10, in <module>
    main()
  File "magic_square.py", line 6, in main
    number = int(input())
ValueError: invalid literal for int() with base 10: 'fifteen'