Python Forum
Can’t get program to run properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can’t get program to run properly
#10
#Purpose: create a program that reads the customer files and determines the person’s age.
# Then display info in letter format.


from datetime import date

def get_customer_info():
    #open file
    customer_file = open("customers.txt", "r")
    lines = customer_file.readline()
    for lines in customer_file:
        name = lines[0]
        street = lines[1]
        city =  lines[2]
        state_zip = lines[3]
        dob = lines[4].split("/")
        pet = lines[4].rstrip("\n")
        age = calculate_age(age)
        discount = get_discount(age)

    customer_file.close()
        
        
    #get the vendor based off of the pet the user has 
    animals = ["Cat","Dog","Horse","Fish"]
    vendors = ["Bertha's Kitty Boutique","K9 Supplies","Saddle & Tack Shoppe","Aquariums 'N' Stuff","Pet-R-Us"]

    if(pet in animals):
        vendorIndex = animals.index(pet)
    else:
        vendorIndex = 4
          
        vendor = vendors[vendorIndex]
    
    customer_file.close()
def calculate_age(birthDate):
    #function calculates the customers age.
    today = date.today()
    age = today.year - birthDate.year - (today.month, today.day) < (birthDate.month, birthDate.day)
    return age

def get_discount(age):       #this function deteremines the customer's discount based off their age
    discount = 0
    if(18 <= age <= 25):
        discount = 20
    elif(26 <= age <= 35):
        discount = 15
    elif(36 <= age <= 45):
        discount = 10
    elif(46 <= age <= 60):
        discount = 5
    elif(60 <= age):
        discount = 25
    return discount

#format letter
def letter(name, street, city, state_zip, dob, pet, discount):
     #format letter
    letterFormat = open("Letter.txt","w") #saving the file name with the name Letter.txt format the letter into another text file.
    letterFormat.write(name,"\n")
    letterFormat.write(street,"\n")
    letterFormat.write(city,",")
    letterFormat.write(state_zip,"\n\n")
    letterFormat.write("Dear Valued Customer:\n\n")
    letterFormat.write(vendor, "Happy Birthday! You are eligible for a ", str(discount), "% discount at")
    letterFormat.write(".\n\nHappy shopping,\n")
    letterFormat.close()
    print(letterFormat)
    
get_customer_info()
I've changed it up a bit and now I am getting this error: age = calculate_age(birthDate)
NameError: name 'birthDate' is not defined
and I'm sure it will do the same with the other varuable in the get_customer_info() for discount how could I use these functions to work with variables?
Reply


Messages In This Thread
Can’t get program to run properly - by ITnet20 - Dec-03-2019, 05:42 AM
RE: Can’t get program to run properly - by buran - Dec-04-2019, 07:10 PM
File issues - by ITnet20 - Dec-04-2019, 05:58 AM
RE: File issues - by buran - Dec-04-2019, 09:45 AM
RE: File issues - by jefsummers - Dec-04-2019, 03:16 PM
RE: File issues - by ITnet20 - Dec-04-2019, 06:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  The program isn't counting the total properly DanielCook 1 1,746 Jul-09-2020, 04:51 PM
Last Post: mrdominikku

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020