Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Can’t get program to run properly ITnet20 Programmer named Tim Posts: 5 Threads: 0 Joined: Dec 2019 Reputation: 0 Likes received: 0 #1 Dec-03-2019, 05:42 AM (This post was last modified: Dec-04-2019, 05:27 AM by Larz60+. Edited 4 times in total.) #Program is written in python from datetime import date def calculateAge(birthDate): today = date.today() age = today.year - birthDate.year - ((today.month, today.day) < (birthDate.month, birthDate.day)) return age def getDiscount(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 file = open("cusoumers.txt", "r") for x in file: data = x.split(",") name = data[0] street = data[1] city = data[2] state = data[3] dob = list(map(int, data[4].split("/"))) pet= data[5].rstrip("\n") age = calculateAge(date(dob[2],dob[1],dob[0])) discount = getDiscount(age) 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 reqVendor = vendors[vendorIndex] f= open(name+"_formLetter.txt","w+") #saving the file name with the _formLetter.txt format f.write(name+"\n") f.write(street+"\n") f.write(city+",") f.write(state+"\n\n") f.write("Dear Valued Customer:\n\n") f.write("Happy Birthday! You are eligible for a "+str(discount)+"% discount at") f.write(reqVendor+".\n\nHappy shopping,\n") f.close() michael1789 Minister of Silly Walks Posts: 327 Threads: 29 Joined: May 2019 Reputation: 29 Likes received: 38 #2 Dec-03-2019, 06:00 AM (This post was last modified: Dec-03-2019, 06:00 AM by michael1789. Edited 3 times in total.) What's wrong with it? It's preferred that you put tags around code so that the tabs and stuff stay. Put a "[ python ]" on the line above your code, and a "[ /python ]" on the line after. Don't put the spaces inside the brackets. There is no way for us to know your indentations and that may be the problem. I did just notice that you are trying to open a file called "cusoumers.txt". Any chance that's not the right name? perfringo Da Bishop Posts: 1,399 Threads: 7 Joined: Jun 2018 Reputation: 143 Likes received: 305 #3 Dec-03-2019, 07:32 AM (This post was last modified: Dec-03-2019, 07:33 AM by perfringo. Edited 1 time in total.) Some observations of this unformatted code: - it's useful to follow PEP8 Naming conventions: Function and Variable Names: Quote:Function names should be lowercase, with words separated by underscores as necessary to improve readability. Variable names follow the same convention as function names. mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility. - it is useful to open files this way `with open('filename.txt', 'r') as f:` - it is useful to unpack list which you get from .split() and use row or line instead of x: ```name, street, city, state = row.split(',') # NB! you have more values per line ```- it's unclear what discount should be applied to age 60 (first condition takes precedence but is it intended this way?) - in calculating discount one could use battaries-included tools (bisect module) and avoid long chain of comparisons: ```from bisect import bisect def get_discount(age): ages = [18, 26, 36, 46, 60] discount = [0, 20, 15, 10, 5, 25] i = bisect(ages, age) return discount[i] ```This assumes that under 18 the discount is zero: ```>>> get_discount(5) 0 >>> get_discount(18) 20 >>> get_discount(25) 20 >>> get_discount(26) 15 >>> get_discount(100) 25 ```- and yes, it would be helpful if (a) code is in tags (b) have better description than 'can't get program to run properly' I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. ITnet20 Programmer named Tim Posts: 5 Threads: 0 Joined: Dec 2019 Reputation: 0 Likes received: 0 #4 Dec-04-2019, 05:25 AM (This post was last modified: Dec-04-2019, 05:43 AM by ITnet20.) So okay spelled the customers.txt file correctly but my goal here is I’m trying to take information for about 4 or 5 lines of data in the customers file and take that data and apply a discount with the ranges above. Also I’m trying to match each customer based of the pet they have which is the last item in each line in the customers.txt file match the appropriate vendor i tried making a list of it and then implementing it into another text file that would write the data out in a letter format as you can see at the bottom there. Does that make sense? I’m doing this for a major programming project and I’m not too great in programming so I’m sorry if some things don’t make a whole lot of sense here I’m just looking for some help. [python] from datetime import date def calculateAge(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 getDiscount(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 def getCustomerInfo(customer_file): #open file customer_file = open("customers.txt", "r") lines = customer_file.readline() for lines in customer_file: data = lines.split(",") name = data[0] street = data[1] city = data[2] state = data[3] zipCode = data[4] dob = data[5] pet = data[6].rstrip("\n") age = calculateAge(birthDate) discount = getDiscount(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] def letter(getCustomerInfo): #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(str(name),"\n") letterFormat.write(str(street),"\n") letterFormat.write(str(city),",") letterFormat.write(str(state),"\n\n") letterFormat.write(str(zipCode),"\n\n") letterFormat.write("Dear Valued Customer:\n\n") letterFormat.write(str(vendor), "Happy Birthday! You are eligible for a ", str(discount), "% discount at") letterFormat.write(".\n\nHappy shopping,\n") letterFormat.close() print(letterFormat) [python] (Dec-04-2019, 05:25 AM)ITnet20 Wrote: So okay spelled the customers.txt file correctly but my goal here is I’m trying to take information for about 4 or 5 lines of data in the customers file and take that data and apply a discount with the ranges above. Also I’m trying to match each customer based of the pet they have which is the last item in each line in the customers.txt file match the appropriate vendor i tried making a list of it and then implementing it into another text file that would write the data out in a letter format as you can see at the bottom there. Does that make sense? I’m doing this for a major programming project and I’m not too great in programming so I’m sorry if some things don’t make a whole lot of sense here I’m just looking for some help. [python] from datetime import date def calculateAge(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 getDiscount(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 def getCustomerInfo(customer_file): #open file customer_file = open("customers.txt", "r") lines = customer_file.readline() for lines in customer_file: data = lines.split(",") name = data[0] street = data[1] city = data[2] state = data[3] zipCode = data[4] dob = data[5] pet = data[6].rstrip("\n") age = calculateAge(birthDate) discount = getDiscount(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] def letter(getCustomerInfo): #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(str(name),"\n") letterFormat.write(str(street),"\n") letterFormat.write(str(city),",") letterFormat.write(str(state),"\n\n") letterFormat.write(str(zipCode),"\n\n") letterFormat.write("Dear Valued Customer:\n\n") letterFormat.write(str(vendor), "Happy Birthday! You are eligible for a ", str(discount), "% discount at") letterFormat.write(".\n\nHappy shopping,\n") letterFormat.close() print(letterFormat) [python] #MADE SOME EDITS JUST FOR FORMAT REASONS BUT STILL NOT WORKING. ITnet20 Programmer named Tim Posts: 5 Threads: 0 Joined: Dec 2019 Reputation: 0 Likes received: 0 #5 Dec-04-2019, 05:58 AM (This post was last modified: Dec-04-2019, 05:58 AM by ITnet20. Edited 1 time in total.) ```#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 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 def get_customer_info(customer_file): #open file customer_file = open("customers.txt", "r") lines = customer_file.readline() for lines in customer_file: data = lines.split(",") name = data[0] street = data[1] city = data[2] state = data[3] zipCode = data[4] dob = data[5] pet = data[6].rstrip("\n") age = calculate_age(birthDate) 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] def letter(get_customer_info): #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(str(name),"\n") letterFormat.write(str(street),"\n") letterFormat.write(str(city),",") letterFormat.write(str(state),"\n\n") letterFormat.write(str(zipCode),"\n\n") letterFormat.write("Dear Valued Customer:\n\n") letterFormat.write(str(vendor), "Happy Birthday! You are eligible for a ", str(discount), "% discount at") letterFormat.write(".\n\nHappy shopping,\n") letterFormat.close() print(letterFormat) ```This is my objective here: ACME will provide you with a file that contains customer names and other personal information (customers.txt). Full Name, Street Address, City, State, Zip, Date of Birth (mm/dd/yyyy), Pet ACME wants your program to read the customer files and determine the person’s age. Then based on the customer’s age determine the personal discount they are eligible for. The discount ranges are: 18-25 yrs = 20% 26-35 yrs = 15% 36-45 yrs = 10% 46-60 yrs = 5% > 60 yrs = 25% The program then matches the customer’s pet preference with a suitable vendor: Cat = Bertha’s Kitty Boutique Dog = K9 Supplies Horse = Saddle & Tack Shoppe Fish = Aquariums ‘N’ Stuff any other = Pet-R-Us The program then generates a form letter text file for each customer. Sample Letter (Customer Name) (Street Address) (City, State Zip) Dear Valued Customer: Happy Birthday! You are eligible for a (percent)% discount at (vendor). Happy shopping, (your name) (today’s date (dynamic, the day I run your program for evaluation) perfringo Da Bishop Posts: 1,399 Threads: 7 Joined: Jun 2018 Reputation: 143 Likes received: 305 #6 Dec-04-2019, 08:01 AM (Dec-04-2019, 05:25 AM)ITnet20 Wrote: MADE SOME EDITS JUST FOR FORMAT REASONS BUT STILL NOT WORKING. You should close code block with [/python]. You could provide some rows from file to understand the initial data. I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame. buran Posts: 6,411 Threads: 105 Joined: Sep 2016 Reputation: 500 Likes received: 1340 #7 Dec-04-2019, 09:45 AM it's not clear how you run your code and call functions you define in the snippet then you have defined `get_customer_info()` function but it does not return anything, so even if you call it at some point it will have no effect also you have `get_customer_info` argument in the signature of `letter` function. Note that it's something different from the function of the same name defined before that. You never use it in the body of the said `letter` function. all the variable like name, street, city, etc. would not be defined when you try to use them jefsummers Verb Conjugator Posts: 628 Threads: 1 Joined: May 2019 Reputation: 63 Likes received: 81 #8 Dec-04-2019, 03:16 PM Lots of work to do, agree with buran. Biggest issue is how you move data around, but there are others. When reading customers.txt you never close the file. Suggest using "with" for both your reading and writing blocks as it nicely handles that for you. With paired info such as the animals and vendors lists, a dictionary is an ideal way to handle the data and get the right vendor with the right animal. In get_customer_info() you take the parameter customer_file but then promptly ignore that by using that variable to open customers.txt. name, address, etc are already strings so you should not need to use str(name). Python won't give an error but the code is redundantly saying the same thing repeatedly. buran likes this post ITnet20 Programmer named Tim Posts: 5 Threads: 0 Joined: Dec 2019 Reputation: 0 Likes received: 0 #9 Dec-04-2019, 06:07 PM (This post was last modified: Dec-04-2019, 06:08 PM by ITnet20. Edited 1 time in total.) ```from datetime import date def calculateAge(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 getDiscount(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 def getCustomerInfo(customer_file): #open file customer_file = open("customers.txt", "r") lines = customer_file.readline() for lines in customer_file: data = lines.split(",") name = data[0] street = data[1] city = data[2] state = data[3] zipCode = data[4] dob = data[5] pet = data[6].rstrip("\n") age = calculateAge(birthDate) discount = getDiscount(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] def letter(getCustomerInfo): #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(str(name),"\n") letterFormat.write(str(street),"\n") letterFormat.write(str(city),",") letterFormat.write(str(state),"\n\n") letterFormat.write(str(zipCode),"\n\n") letterFormat.write("Dear Valued Customer:\n\n") letterFormat.write(str(vendor), "Happy Birthday! You are eligible for a ", str(discount), "% discount at") letterFormat.write(".\n\nHappy shopping,\n") letterFormat.close() print(letterFormat) ``` ITnet20 Programmer named Tim Posts: 5 Threads: 0 Joined: Dec 2019 Reputation: 0 Likes received: 0 #10 Dec-04-2019, 06:16 PM (This post was last modified: Dec-04-2019, 06:16 PM by ITnet20. Edited 1 time in total.) ```#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? « Next Oldest | Next Newest »

Top Page

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

Forum Jump:

Users browsing this thread: 1 Guest(s)