Question

In: Computer Science

In this python script find any bugs and errors which would cause the code to crash....

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()

Solutions

Expert Solution

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"'''


Related Solutions

For each of the following Python errors provide a scenario that would cause it: TypeError AttributeError...
For each of the following Python errors provide a scenario that would cause it: TypeError AttributeError IndexError ValueError ZeroDivisionError IndentationError
#python. Explain code script of each part using comments for the reader. The code script must...
#python. Explain code script of each part using comments for the reader. The code script must work without any errors or bugs. Ball moves in a 2D coordinate system beginning from the original point (0,0). The ball can move upwards, downwards, left and right using x and y coordinates. Code must ask the user for the destination (x2, y2) coordinate point by using the input x2 and y2 and the known current location, code can use the euclidean distance formula...
Find and fix the compile time bugs in the code at the end of this section....
Find and fix the compile time bugs in the code at the end of this section. Compile time bugs show as errors when you compile, but the Visual Studio IDE also gives you visual clues in the form of red squiggly underlines, as shown here. This assignment is meant to test your attention to detail and strengthen your debugging skills. Here is the code. // Week 4 Assignment-1 // Description: Compile time errors //---------------------------------- //**begin #include files************ #include <iostream> //...
#python #code #AP class #Tech write a function code script which will print out number pyramid...
#python #code #AP class #Tech write a function code script which will print out number pyramid in the form of * so the output will be made up of **** resting on top of each other to form a pyramid shape. Bottom layer should be made of 5 multiplication signs like ***** then next 4 multiplication signs and so on. Top part should have only one *
Explain the errors in the C code that cause the output NOT to be -10. (Hint:...
Explain the errors in the C code that cause the output NOT to be -10. (Hint: There are 2 errors): #include <stdio.h> #include <stdlib.h> #include <pthread.h>    #define SIZE 5 void *threadFunc(void *arg); int *changingVal; int a[SIZE]; // Assume a[] = { 13444, 3320, 31020, 3302, 31313 }; int main() { int i; int min; int * changeVal = malloc (sizeof(int*)); *changeVal = -10; // Thread creation pthread_t thread1; pthread_attr_t attr; pthread_attr_init(&attr);    for (i = 0; i < SIZE;...
Which of the following errors would cause the adjusted trial balance to not​ balance? A. posting...
Which of the following errors would cause the adjusted trial balance to not​ balance? A. posting the debit for accrued interest to insurance expense B. reversing the debits and credits in an adjusting entry C. failure to post one side of a journal entry D. omitting the adjusting entry for depreciation expense 2. Permanent accounts do not include​ ________. A. Rent Expense B. Interest Receivable C. Prepaid Insurance D. Taxes Payable
Which of the following errors would cause the trial balance to be out of​ balance? A.The...
Which of the following errors would cause the trial balance to be out of​ balance? A.The payment of an account payable for $200 was recorded as a debit to Accounts Payable for $200 and a credit to Cash for $2,000. B.The payment of an account payable for$400 was recorded as a debit to​ Cash, $400​,and a credit to Accounts​ Payable, $400. C.The payment of utilities expense was recorded as a debit to Rent Expense for $77and a credit to Cash...
Which of the following errors, each considered individually, would cause the trial balance totals to be...
Which of the following errors, each considered individually, would cause the trial balance totals to be unequal? a.Cash received from customers on account was posted as a debit of $720 to Cash and a credit of $720 to Accounts Payable. b.A payment of $4,450 to a creditor was posted as a debit of $4,500 to Accounts Payable and a credit of $450 to Cash. c.A payment of $67 for insurance was posted as a debit of $76 to Prepaid Insurance...
Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
how to write a code with python function trying to find minimum difference between any two...
how to write a code with python function trying to find minimum difference between any two elements in a list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT