Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this code for an address book. You can add, delete, etc the names and inf

ID: 3747623 • Letter: I

Question

I have this code for an address book. You can add, delete, etc the names and information of the entries. I am trying to add the variable "stamp" where it will add or subtract to the variable depending upon the input (add or delete) and print a notice of how many stamps as well as how much it would cost for mailing cards. I can't seem to get it to work. At its present state, i have made "stamp" a global but still get a NameError: name 'stamp' is not defined. please help.

import pickle
stamps = 0

def open_file():
    output_file = open('cardList.dat', 'wb')
    return output_file

def make_cardList():
    cardList = {}
    option = 'y'
      
    while option == 'y':
        name = input('Enter a name:' )
        address = input('Enter their entire address, separating sections with commas: ')
        name = name.lower()
        address = address.lower()
        cardList[name] = address
        option = input('Enter "Y" to add more or "N" to edit list: ')
        option = option.lower()
        global stamps
        stamps +=1
      
    return cardList

def menu(cardList):
    option = 'y'
  
    while option == 'y':
        choose = input('Choose your next action: "l" to look up, "a" to add,'
                            '"e to edit or "d to delete: " If finished, enter "n" for none: ')
        choose = choose.lower()
      
        if choose == 'l':
            cardList = lookUp(cardList)

        elif choose == 'a':
            cardList = add(cardList)
                                  
        elif choose == 'c':
            cardList = change(cardList)

        elif choose == 'd':
            cardList = delete(cardList)
                  
        elif choose == 'n':
            print('All done.')
            option = 'n'
          
        else:
            print("Incorrect entry. Please review options and try again")
          
def lookUp(cardList):
    name = input('Enter a name to look up their information: ')
    name = name.lower()
    print(cardList[name])
    return cardList

def add(cardList):
    name = input('Enter a name to add their information: ')
    address = input('Enter their entire address, separating sections with commas: ')
    name = name.lower()
    address = address.lower()
    cardList[name] = address
    return cardList
    global stamps
    stamps +=1

def change(cardList):
    name = input('Enter a name to change their information: ')
    name = name.lower()
    print(cardList[name])
    option = input('Enter "y" to change this address: ')
    option = option.lower()
  
    if option == 'y':
        address = input('Enter their entire address, separating sections with commas: ')
        address = address.lower()
        cardList[name] = address  
    return cardList

def delete(cardList):
    name = input('Enter a name to delete their information: ')
    name = name.lower()
    print(cardList[name])
    option = input('Enter "y" to delete this address: ')
    option = option.lower()
    if option == 'y':
        del cardList[name]
        return cardList
    global stamps
    stamps -=1

output_file = open_file()
cardList = make_cardList()
menu(cardList)
pickle.dump(cardList, output_file)
output_file.close()
global stamp
print("You will need", (stamp), "stamp(s) at a cost of $ {0:,.2f}.".format (stamp * .50))

Explanation / Answer

After defining the global variable,please initialise the variable as

global stamp
stamp=stamps ##Iniatialization of a variable is required

As declaring the variable is just the allocation of some random memory to the gobal variable. It shall be iniatlised to be properly usable(readable,writable).

Your code changes as given below after initialization of variable.

import pickle
stamps = 0

def open_file():
output_file = open('cardList.dat', 'wb')
return output_file

def make_cardList():
cardList = {}
option = 'y'
  
while option == 'y':
name = input('Enter a name:' )
address = input('Enter their entire address, separating sections with commas: ')
name = name.lower()
address = address.lower()
cardList[name] = address
option = input('Enter "Y" to add more or "N" to edit list: ')
option = option.lower()
global stamps
stamps +=1
  
return cardList

def menu(cardList):
option = 'y'
  
while option == 'y':
choose = input('Choose your next action: "l" to look up, "a" to add,'
'"e to edit or "d to delete: " If finished, enter "n" for none: ')
choose = choose.lower()
  
if choose == 'l':
cardList = lookUp(cardList)

elif choose == 'a':
cardList = add(cardList)
  
elif choose == 'c':
cardList = change(cardList)

elif choose == 'd':
cardList = delete(cardList)
  
elif choose == 'n':
print('All done.')
option = 'n'
  
else:
print("Incorrect entry. Please review options and try again")
  
def lookUp(cardList):
name = input('Enter a name to look up their information: ')
name = name.lower()
print(cardList[name])
return cardList

def add(cardList):
name = input('Enter a name to add their information: ')
address = input('Enter their entire address, separating sections with commas: ')
name = name.lower()
address = address.lower()
cardList[name] = address
return cardList
global stamps
stamps +=1

def change(cardList):
name = input('Enter a name to change their information: ')
name = name.lower()
print(cardList[name])
option = input('Enter "y" to change this address: ')
option = option.lower()
  
if option == 'y':
address = input('Enter their entire address, separating sections with commas: ')
address = address.lower()
cardList[name] = address  
return cardList

def delete(cardList):
name = input('Enter a name to delete their information: ')
name = name.lower()
print(cardList[name])
option = input('Enter "y" to delete this address: ')
option = option.lower()
if option == 'y':
del cardList[name]
return cardList
global stamps
stamps -=1

output_file = open_file()
cardList = make_cardList()
menu(cardList)
pickle.dump(cardList, output_file)
output_file.close()
global stamp
stamp=stamps##initialize your variable as per your requirement
print("You will need", (stamp), "stamp(s) at a cost of $ {0:,.2f}.".format (stamp * .50))

##Please let me know if you have any concers.