2

I am trying to set a global variable of 'help' to display a function that I have in my code. The goal is anytime the user types in the word 'help' I can display my table I have created. The function is called table() that I want to display. My code is as follows:

def table():
    header=(" Operation      Command-line Format")
    underline=("_________      ___________________")
    meat=("Union                   set1&set2 \n Intersection            set1|set2 \n Difference              set1-set2 \n Symmetric difference    set1 ^ set2 \n Cartesian product       set1 X set2 \n Subset                  set1<=set2 \n Proper subset           set1<set2 \n Super set               set1=>set2 \n Proper super set        set1>set2 \n Cardinality             card set1 \n Membership-in           xE set1      where x is single element\n Membership-not-in       x!Eset1      where x is single element\n Power set               power set1\n Display this table      help") 
    print(header, "\n",underline,"\n",meat) 




def intro():


    firstChoice=input("Would you like to get the sets from a file or input your own? \n Press 1 for from file \n Press 2 to input your own")
    if input is 'help':
        table()

    if firstChoice== '1':
        fileChoice=input("Please enter the fle you would like to open")


        with open(fileChoice, 'r+') as f:

            z=f.readlines()
            length=len(z)
            try:
                if z != len(z):
                    fileSet1=z[length-length]
                    if length-length != " ":
                        fileSet1=fileSet1.strip('\n')
                        fileSet1=str(fileSet1)
                        fileSet1.split(',')
                        fileSet1=set([fileSet1])
                    fileSet2=z[length-length+1]
                    if length-length+1 != " ":
                        fileSet2=fileSet2.strip('\n')
                        fileSet2=str(fileSet2)
                        fileSet2.split(',')
                        fileSet2=set([fileSet2])
                    fileSet3=z[length-length+2]
                    if length-length+2 != " ":
                        fileSet3=fileSet3.strip('\n')
                        fileSet3=str(fileSet3)
                        fileSet3.split(',')

                        fileSet3=set([fileSet3])


                    fileSet4=z[length-length+3]
                    if length-length +3 != " ":
                        fileSet4=fileSet4.strip('\n')
                        fileSet4=str(fileSet4)
                        fileSet4.split(',')

                        fileSet4=set([fileSet4])


                    print ("This is fileSet1 \n",fileSet1)
                    print ("This is fileSet2 \n",fileSet2)
                    print ("This is fileSet3 \n",fileSet3)
                    print ("This is fileSet4 \n",fileSet4)

            except:
                try:
                    print ("This is fileSet1 \n",fileSet1,"\nThis is fileSet2 \n",fileSet2,"\nThis is fileSet3 \n",fileSet3)
                except:
                    print ("This is fileSet1 \n",fileSet1,"\nThis is fileSet2 \n",fileSet2)


            f.close()





    elif firstChoice== '2':
        keep_going= 'yes'
        userSet=input("Please enter the set you would like to use. " )
        userSet1=userSet.split(',')
        userset1=set(userSet1)





        print("User set 1 ", userset1)
        keep_going=input("would you like to go again? Please respond with yes or no")
        while keep_going == 'yes':
            userSet=input('what other set would you like to use? ')
            userSet2=userSet.split(',')
            userset2=set(userSet2)



            print ("user set 2 ", userset2)
            keep_going=input ("Would you like to use another set too? ")
            if keep_going== 'yes':
                userSet=input("Th other set you would like to use?")
                userSet3=userSet.split(',')
                userset3=set([userSet3])
                print("user set 3 ", userset3)
                keep_going=input("would you like to keep going? ")
                if keep_going== 'yes':
                    userSet=input("Th other set you would like to use?")
                    userSet4=userSet.split(',')
                    userset4=set([userSet4])
                    print("user set 4 ", userset4)
                    keep_going==input("Another one?")
                    if keep_going == 'yes':
                        print ("i don't think we need another set")
                        keep_going='no'


    firstSet=input("which set would you like to use for operations \nplease type in userset1,userset2, userset3, userset4 or \n                fileSet1,fileSet2,fileSet3 or fileSet4  ")
    if firstSet == "userset1":
        set1=userset1


        print ("this is set1",set1)
    elif firstSet =="userset2":
        set1=userset2

        print ("this is set1",(set1))
    elif firstSet =="userset3":
        set1=userset3

        print ("this is set1",(set1))
    elif firstSet =="userset4":
        set1=userset4

        print ("this is set1",(set1))
    elif firstSet =="fileSet1":
        set1=fileSet1

        print ("this is set1",(set1))
    elif firstSet =="fileSet2":
        set1=fileSet2

        print ("this is set1",(set1))
    elif firstSet =="fileSet3":
        set1=fileSet3

        print ("this is set1",(set1))
    elif firstSet =="fileSet4":
        set1=fileSet4

        print ("this is set1",(set1))
    else:
        print ('your bad')


    secondSet=input("What other set would you like to use for operations \n please type in userset1,userset2, userset3, userset4, setFile ")
    if secondSet == "userset1":
        set2=userset1

        print ("this is set2",set2)
    elif secondSet =="userset2":
        set2=userset2


        print ("this is set2",(set2))
    elif secondSet =="userset3":
        set2=userset3

        print ("this is set2",(set2))
    elif secondSet =="userset4":
        set2=userset4

        print ("this is set2",(set2))
    elif secondSet =="fileSet1":
        set2=fileSet1

        print ("this is set1",(set1))
    elif secondSet =="fileSet2":
        set2=fileSet2

        print ("this is set1",(set1))
    elif secondSet =="fileSet3":
        set2=fileSet3

        print ("this is set1",(set1))
    elif secondSet =="fileSet4":
        set2=fileSet4

        print ("this is set1",(set1))

    else:
        print ('your bad')


    table()



    commandinput=input("please select the operation you would like to do")
    commandinput=commandinput.split()
    if '&' in commandinput:
        union=set1.union(set2)
        print(union)
    elif '|' in commandinput:
        intersetction=set1.intersection(set2)
        print(intersection)
    elif '-' in commandinput:
        difference=set1/difference(set2)
        print(difference)
    elif '^' in commandinput:
        symmetric=set1.symmetric_difference(set2)
        print(symmetric)
    elif 'X' in commandinput:
        print ("HIGH")

    elif '<=' in commandinput:
        subset=set2.issubset(set1)
        print(subset)
    elif '<' in commandinput:
        print ("HIGH")
    elif '=>' in commandinput:
        subset=set2.issuperset(set1)
        print(subset)
    elif '>' in comandinput:
        print ("HIGH")
    elif 'card' in commandinput:
        length=len(set1)
        print(length)
    elif 'E' in commandinput:

        x=input("what would you like to search for")
        if x in set1:
            print ( "It is in here!!")
        else:
            print("It isn't in here")

    elif '!E' in commandinput:
        x=input("what would you like to search for ")
        if x in set1:
            print ("It is in here")
        else:
            print ("Not here!")
    elif "power" in commandinput:
        print ("HIGH")
    elif 'help' in commandinput:
        print(table())









intro()                                
8
  • 9
    This code makes me want to cry. You have a lot of problems here, and the biggest one is this: any time you are copying and pasting (or repeating in any fashion) chunks of code, it means you could do it in a better way. This is full of copy/paste coding. You also want to seriously look at your code structure and rearrange it - what you want to do will require you to set up your input independent of what you do, and dispatch to the appropriate code based on that input. Commented Apr 25, 2012 at 23:59
  • @Lattyware Yea I know that my code is pretty terrible I had it broken up into functions but, My instructor will be using a file with sets, and I don't know how many sets she will be using. So I can't pass the necessary variables into other functions correctly. With my lack of knowledge. Commented Apr 26, 2012 at 0:05
  • 3
    This is why data structures like lists exist - you can have a list of items and perform operations on all of them. Again, a good indicator you should be using a list is where you have variable1 through variable5 or whatever. Commented Apr 26, 2012 at 0:07
  • @Lattyware is there a place where I can go to figure out. "to set up your input independent of what you do, and dispatch to the appropriate code based on that input", or is it called something specific. Sorry im a python noob. Commented Apr 26, 2012 at 0:12
  • You are asking me to explain how to structure your code and the real answer is you need to sit down and learn Python. You are clearly lacking knowledge of a number of core concepts, and what you are currently trying to do isn't going to end well with what you know. Sit down and try simpler things, and learn what you can do in Python, and how to do it. Giving you more answers here won't really help solve your real problem. Look at a tutorial. Commented Apr 26, 2012 at 0:14

2 Answers 2

5

You should use ==, not is. is tests if the strings are the exact same object in memory, which they probably aren't.

if firstChoice == 'help':

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

Comments

1

I think you are checking against the wrong variable name.

firstChoice=input("Would you like to get the sets from a file or input your own? \n Press 1 for from file \n Press 2 to input your own")
if input is 'help':
    table()

Should be

firstChoice=input("Would you like to get the sets from a file or input your own? \n Press 1 for from file \n Press 2 to input your  own")
if firstChoice is 'help':
    table()

1 Comment

@bradb: I get the feeling that you are trying to write a big chunk of code in one go. You need to write the program one bit at a time, and make sure that bit works, before adding the next bit. Otherwise you end up having loads of errors at once, and it will be impossible for you to figure out what you are doing wrong, because changing one thing will not make it better.

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.