Faiq Ashraf
Faiq Ashraf

Reputation: 49

Wanting to collect user input while outputting in a specific format [python]

So I am creating a program that allows the user to input values into sets a-f and you for the universal set. I want to collect their input and have it outputted in a specific format. For example:

Which set would you like to edit? (a-f) or universe (u) >> a
Editing set a. Please enter values, when finished, enter 'stop' 

give me something >>> a
I have added a to the set a.

give me something >>> b
I have added b to the set a.

give me something >>> c
I have added c to the set a.

give me something >>> ab
I have added ab to the set a.

give me something >>> ac
I have added ac to the set a.

give me something >>> bc
I have added bc to the set a.

give me something >>> abc
I have added abc to the set a.

give me something >>> stop
a = ["a", "b","c","ab","ac","bc","abc"]

Instead, this is what happens:

Welcome to Logica!

Which set would you like to edit? (a-f) or universe (u) >> a
Editing set a. Please enter values, when finished, enter 'stop' 

give me something >>> a
I have added a to the set a.

give me something >>> b
I have added b to the set a.

give me something >>> c
I have added c to the set a.

give me something >>> ab
I have added ab to the set a.

give me something >>> ac
I have added ac to the set a.

give me something >>> bc
I have added bc to the set a.

give me something >>> abc
I have added abc to the set a.

give me something >>> stop
['a', 'b', 'c', 'a', 'b', 'a', 'c', 'b', 'c', 'a', 'b', 'c']

As you can see above, my code separates each letter when inputted. Is there a way to get my desired output in the same format as I specified above? Here is my code that I have right now:

class Logica:

    def start(self):
        # welcome user
        print("Welcome to Logica!\n")

        # seek user input
        # a,b,c,d,e,f = [],[],[],[],[],[]
        self.a = []
        self.b = []
        self.c = []
        self.d = []
        self.e = []
        self.f = []
        self.u = [] # universal set

        setEdit = input("Which set would you like to edit? (a-f) or universe (u) >> ")
        if setEdit == "a":
            print("Editing set a. Please enter values, when finished, enter 'stop' \n")
            entry = ""
            while True:
                entry = input("give me something >>> ")
                if entry != "stop": 
                    self.a += entry
                    print("I have added " + entry + " to the set a.\n")
                else:
                    # display values in a
                    aa = []
                    for response in self.a:
                        # print(str(response) + ",")
                        aa += str(response)
                    print(aa)
                    a.whatNow()

        elif setEdit == "b":
            print("Editing set b. Please enter values, when finished, enter 'stop' \n")
            entry = ""
            while True:
                entry = input("give me something >>> ")
                if entry != "stop": 
                    self.b += entry
                    print("I have added " + entry + " to the set b.\n")
                else:
                    # display values in b
                    bb = []
                    for response in self.b:
                        # print(str(response) + ",")
                        bb += str(response)
                    print(bb)
                    a.whatNow()

    def whatNow(self):
        print("Here are your options: \n")
        exit(0)       



if __name__ == "__main__":
    a = Logica()
    a.start()

Upvotes: 0

Views: 43

Answers (1)

jizhihaoSAMA
jizhihaoSAMA

Reputation: 12672

A little hard to understand your code, Change your while loop to:

while True:
    entry = input("give me something >>> ")
    if entry != "stop":
        self.a.append(entry)
        print("I have added " + entry + " to the set a.\n")
    else:
        # display values in a
        aa = []
        for response in self.a:
            # print(str(response) + ",")
            aa.append(response)
        print(aa)
        a.whatNow()

and b,too.

Upvotes: 1

Related Questions