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 - 431 Views - Last Post: 22 November 2012 - 12:14 PM
#1
Functions!
Posted 21 November 2012 - 06:50 PM
Replies To: Functions!
#2
Re: Functions!
Posted 21 November 2012 - 10:25 PM
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
---- 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
#################################
## 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
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
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
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
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
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
#12
Re: Functions!
Posted 22 November 2012 - 12:00 PM
#13
Re: Functions!
Posted 22 November 2012 - 12:14 PM
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
|
|

New Topic/Question
Reply



MultiQuote





|