0

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:

  • a)create a set of all the unique words in file1 and print them
  • b)create a set of all the unique words in file2 and print them
  • c)print all of the words that are contained in both files
  • d)print all of the unique words that are contained in either file
  • e)print all of the words that are contained in file1 but not file2
  • f)print all of the words that are contained in file2 but not file1
  • g)print all of the words that are contained in either file but not both

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:

  • "Now is the time for all good men to come to the aid of their party."

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()
    

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

appreciate it mate. Was wondering if I wasn't thinking logically. And thanks for going the extra mile and explaining the logic of why thats the way you do it. Makes perfect sense when you think about it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.