def build_dictionary(): for line in infile: if line!='' or line[0]!='-': lst=line.split(",") count=count+1 while "" in lst: lst.remove("") count_dict={} alist=[] alist=astring.split() for lastname in alist: if lastname not in count_dict: count_dict[lastname]=1 else: count_dict[lastname]=count_dict[lastname]+1 print(count_dict) for k, v in count_dict.items(): retur(k,"occurs",v,"times.") def main(): import os.path while True: try: name1=input("Enter input name:") infile=open(name1,"r") break except: print("Error in code") main()
Functions!
Page 1 of 112 Replies - 1306 Views - Last Post: 22 November 2012 - 12:14 PM
#1
Functions!
Posted 21 November 2012 - 06:50 PM
I want my program to to have a main function that opens and read a file(which I have running properly), then calls the build_dictionary function(not quite sure how), which takes the file reads the last names and count how times they occur(which I also have running). The build_dictionary function ignores blank lines and any lines that do not contain a person’s name,(not sure if I did that correctly.) Functions aren't my favourite thing, can some help me with this.
Replies To: Functions!
#2
Re: Functions!
Posted 21 November 2012 - 10:25 PM
What does the input data look like? The check for a blank line is
len(line.strip()) > 0
You have a lot of unnecessary code in your function. Start by printing each line as it is read, split it and print that and then figure out how to determine where the last name is or if there is no last name. Post back with some updated code to do this much at least.
len(line.strip()) > 0
You have a lot of unnecessary code in your function. Start by printing each line as it is read, split it and print that and then figure out how to determine where the last name is or if there is no last name. Post back with some updated code to do this much at least.
#3
Re: Functions!
Posted 21 November 2012 - 10:31 PM
---- city: Edmonton
---- province: Alberta
Smith, Raymond
Szczesny, Julia
Smith, Rachel
Barbosa, Denilson
Tremblay, Sophie
Wilson, Gregory
Walters, Mark
---- city: Vancouver
---- province: BC
Harvey, Karen
Li, Russ
Smith, Michael
Ng, Andre
Barbosa, Anita
Turner, Jeremy
Lam, Annette
this is my input data......
---- province: Alberta
Smith, Raymond
Szczesny, Julia
Smith, Rachel
Barbosa, Denilson
Tremblay, Sophie
Wilson, Gregory
Walters, Mark
---- city: Vancouver
---- province: BC
Harvey, Karen
Li, Russ
Smith, Michael
Ng, Andre
Barbosa, Anita
Turner, Jeremy
Lam, Annette
this is my input data......
#4
Re: Functions!
Posted 22 November 2012 - 09:01 AM
You can not solve this problem because you did not print any of the data along the way so you don't know what it looks like = where to look for the last name. Calling functions with parameters.
################################# ## NOT Tested ################################# def build_dictionary(infile): count_dict={} for line in infile: line = line.strip() if len(line) and line[0]!='-': lst=line.split(",") print "list is", lst lastname = lst[0].strip() if lastname not in count_dict: count_dict[lastname]=1 else: count_dict[lastname]=count_dict[lastname]+1 print(count_dict) def main(): while True: try: name1=input("Enter input name:") infile=open(name1,"r") build_dictionary(infile): break except: print("Error in code") main()
#5
Re: Functions!
Posted 22 November 2012 - 09:02 AM
infile is declared locally, so build_dictionary() has no way of knowing what it is. It needs to take it as a parameter, like this:
Then, you can call it like this, in your main function:
Also, in the last line of build_dictionary(), you have a typo - you've written retur instead of return.
def build_dictionary(infile): ...
Then, you can call it like this, in your main function:
result = build_dictionary(infile)
Also, in the last line of build_dictionary(), you have a typo - you've written retur instead of return.
#6
Re: Functions!
Posted 22 November 2012 - 09:35 AM
I tried the first half of the code and it works much nicer and its easier, but when you put everything together it doesn't work I get error in code, I've tried inputting many files that i have but i keep getting the same thing
output:
def build_dictionary(infile): count_dict={} for line in infile: line=line.strip() if len(line) and line[0]!="-": lst=line.split(",") lastname=lst[0].strip() if lastname not in count_dict: count_dict[lastname]=1 else: count_dict[lastname]=count[lastname]+1 def main(): import os.path while True: try: name1=input("Enter input name:") infile=open(name1,"r") result=build_dictionary(infile) break except: print("Error in code") main()
output:
Enter input name:animals.txt Error in code Enter input name:
#7
Re: Functions!
Posted 22 November 2012 - 10:03 AM
Yeah. Okay. So you're clearly using Python 2.
In Python 2, input() tries to evaluate whatever you type in as Python code. You should use raw_input() instead, which returns the input as a string. Or you could use Python 3, where input() does as raw_input() in Python 2, returning a string.
In Python 2, input() tries to evaluate whatever you type in as Python code. You should use raw_input() instead, which returns the input as a string. Or you could use Python 3, where input() does as raw_input() in Python 2, returning a string.
#8
Re: Functions!
Posted 22 November 2012 - 10:09 AM
Im using python 3,
This part of the code works fine alone, but once you add the build_dictionary function, I get the "error in code", I dont get whats happening
def main(): import os.path while True: try: name1=input("Enter input name:") infile=open(name1,"r") break except: print("Error in code") main()
This part of the code works fine alone, but once you add the build_dictionary function, I get the "error in code", I dont get whats happening
#9
Re: Functions!
Posted 22 November 2012 - 10:15 AM
You're catching the error that tells you what the problem is. You'll get "Error in code" instead of a pretty stack trace, use that to find out what your errors are.
As a rule of thumb, you should only catch things that are beyond your control, not the result of a programming bug. Check if the file exists first, and the program is logically correct instead of leaving it to chance and giving a vague error message.
As a rule of thumb, you should only catch things that are beyond your control, not the result of a programming bug. Check if the file exists first, and the program is logically correct instead of leaving it to chance and giving a vague error message.
This post has been edited by Simown: 22 November 2012 - 10:17 AM
#10
Re: Functions!
Posted 22 November 2012 - 10:28 AM
import os.path while True: try: name1=input("Enter input name:") infile=open(name1,"r") name2=input("Enter output name:") while(os.path.isfile(name2)): name2=input("File exists. Enter new output name:") ofile=open(name2, "w") break except: print("Error in code")
Same code, except I'm gonna write a new file
output:
Enter input name:animals.txt Enter output name:animals.y
now im gonna use what i have in this program
output:
Enter input name:animals.txt Error in code Enter input name:
I cant figure out why.
Im guess this is happening because of how Im calling the build_dict function. Do you know of any other ways I can call it.
#11
Re: Functions!
Posted 22 November 2012 - 11:22 AM
Take the try-except block out for a minute. What does it do then? I am guessing it will probably help you pinpoint the error by printing a stack trace of the Python program, and what caused an error to be caught.
#12
Re: Functions!
Posted 22 November 2012 - 12:00 PM
I figured it out, there was an error in the build_dic fun., I guess if theres an error there it effect the entire code, semantic error.
#13
Re: Functions!
Posted 22 November 2012 - 12:14 PM
So the program is running, but its not iterating through the entire file, Im thinking i should make a list and then
does tht make sense?
for lastname in alist: if lastname not in count_dict: count_dict[lastname]=1 else: count_dict[lastname]=count_dict[lastname]+1 return count_dict
does tht make sense?
This post has been edited by medaa: 22 November 2012 - 12:14 PM
Page 1 of 1