Reputation: 23
Probably a duplicate but after 15 min I couldn't find an answer to my question, probably my poor coding terminology.
what is it about my logic that I'm incorrectly passing my arguments? Please refrain from chastising my choice code itself, I'm not very good at this and I know it.
The program is supposed to:
Items (a) and (b) should be produced by the function that creates the sets; there should be a separate function for each of the others. btw is case it somehow helps file 1 is:
file 2 is:
"It is time to go to the party."
def main():
file1()#obtain file1 uniquness
file2()#obtain file2 uniqueness
print("C) Words that appear in both files: "+str(set_1&set_2)+'\n')#addd them
print("D) Unique words that appear in either file: "+str(set_1|set_2)+'\n')
print("E) Words that appear in file1 but not file2: "+str(set_1-set_2)+'\n') #subtract words found in 2 from 1
print("F) Words that appear in file2 but not file1: "+str(set_2-set_1)+'\n')
print("G) Words that appear in either file but not both: "+str(set_1^set_2)+'\n')
def file1():
file_1=open('file1.txt', 'r')
file_1=file_1.readline()
setlist_1=file_1.split()#convert file 1
set_1=set()
for word in setlist_1:
word=word.lower()
word=word.rstrip("\n")#process uniqueness
set_1.add(word)
print("A) Unique words in file1: "+str(set_1)+'\n')#print A
return set_1
def file2():
file_2=open('file2.txt','r')
file_2=file_2.readline()#convert file 2
setlist_2=file_2.split()
set_2=set()
for words in setlist_2:
words=words.lower()#process uniqueness
words=words.rstrip("\n")
set_2.add(words)
print("B) Unique words in file2: "+str(set_2)+'\n')#print B
return set_2
main()
Upvotes: 0
Views: 35
Reputation: 35069
Variables that you use inside your file1
and file2
functions are invisible to other functions, so that they can use the same names for slightly different purposes without interfering with each other. This means that your main
function can't see set_1
and set_2
from those functions. The good news is that you've already happened on the way to hand those objects back to the calling function: by use of return
. You're using it exactly right in those two functions, you just need to link it up in main
- the function call there evaluates to the value the function returns, so to get it to work you just need to do this:
set_1 = file1()
set_2 = file2()
You can call these two variables whatever you want - they don't need to match the name of the variable you return
inside the function.
Upvotes: 2