In: Computer Science
In this python script find any bugs and errors which would cause the code to crash. The code must be re-written correctly.
After debugging make a separate list of all the errors
you found in the script.
contacts_list=[] # global variable, list of contacts_list, one string per contact
def pause()
""" pauses program e.g. to view data or message
"""
input("press enter to continue")
def load():
""" populate list with data
"""
contacts_list.append(('Milo ',
'063847489373'))
contacts_list.append(('Tony', '46483848')
contacts_list.append(('Betty'
'03747384043'))
print("%s records have been loaded"
%(len(contacts_list)))
add():
""" adds contact to list """
name = input("enter name : ")
phone_no = int(input("enter phone number :
"))
contacts_list.append(((phone_no, name)))
def view():
""" displays contacts_list """
index = 0
for contact in contacts_list:
print(index,"
",contact[0], " ", contact[2])
index += 1
pause()
def delete():
""" removes contact based upon index """
index=input("enter index of contact to delete
")
del contacts_list[index-1]
def menu():
""" loops menu of options and does these until
user quits """
while True:
print("")
print(" enter for
option")
print(" =
==========")
print(" v
view contacts_list")
print(" a
add contact")
print(" d
delete contact")
print(" q
quit")
option=input("your
option: ")
optoin=option.lower() #
always lower case
if option == "v":
#view()
elif option ==
"d":
delete()
elif option ==
"a":
add()
elif option ==
"q":
return # exit while loop and menu function
else:
print("invalid menu option")
pause()
def main():
""" entry point if module imported or run as
script """
load()
menu()
if __name__ == "__main__":
# runs program if executed as a script
main()
contacts_list=[] # global variable, list of contacts_list, one string per contact
def pause():#**Didnt have colon
""" pauses program e.g. to view data or message
"""
input("press enter to continue")
def load():
""" populate list with data """
contacts_list.append(('Milo ',
'063847489373'))
contacts_list.append(('Tony', '46483848'))
contacts_list.append(('Betty'
,'03747384043'))#**No comma separating the values of the
tuple
print("%s records have been loaded"
%(len(contacts_list)))
def add():# No def
""" adds contact to list """
name = input("enter name : ")
phone_no = int(input("enter phone number :
"))
contacts_list.append(((name, phone_no)))#**Order
of name and phone_no flipped
def view():
""" displays contacts_list """
index = 0
for contact in contacts_list:
print(index,"
",contact[0], " ", contact[1])#**print contact[0] and contact[1],
not contact[2]
index += 1
pause()
def delete():
""" removes contact based upon index """
index=int(input("enter index of contact to
delete "))#**need to convert index to int since input returns a
string
del contacts_list[index]#**Delete index value
since it starts from 0.No need for index-1
def menu():
""" loops menu of options and does these until
user quits """
while True:
print("")
print(" enter for
option")
print(" =
==========")
print(" v
view contacts_list")
print(" a
add contact")
print(" d
delete contact")
print(" q
quit")
option=input("your
option: ")
option=option.lower() #
always lower case.**option incorrectly written as optoin
if option == "v":
view()#**required code was commented
elif option ==
"d":
delete()
elif option ==
"a":
add()
elif option ==
"q":
return # exit while loop and menu function
else:
print("invalid menu option")
pause()
def main():
""" entry point if module imported or run as
script """
load()
menu()
if __name__ == "__main__":
# runs program if executed as a script
main()
'''The corrections done have been added as a single line comment adjacent to the line they were found starting with '**'. Eg "#**required code was commented"'''