I know this is like an idiot question, but i'm like totatlly stumped by it for some reason. First and foremost, i'm a total newbie at coding(1st month and would be grateful if someone could at least help me with the first part of the project to help me get started:

Objective:
(1st part) A function called (read set) to read in the elements of a set from the user and return the set (stored as a Python list). The function does not have any parameters. The function should ask the user to enter the elements of the set separated by spaces. After the user types in the elements of the set, create a list containing the elements of the set. Hint: Use split(). Your function must remove any repeating items from the set (the user may accidentally enter the same element twice). Make sure the function returns the list via a return statement. Here is a sample run of the function read set:

``````Enter the elements of the set (separated by spaces): 34354
A set with 3 elements has been read (repeating items were removed)

After everything is completed, it's to look something like:
Enter two basic sets and i will print the result of some basic set operations on those sets.

Enter the elements of the set(separated be spaces):
34354

A set with 3 elements has been read (repeating items were removed).

Enter the elements of the set (separated by spaces):
1425

A set with 4 elements has been read.

The two sets you entered are as follows: {3, 4, 5} {1, 4, 2, 5}

Union: {3, 4, 5, 1, 2}
Intersection: {4, 5}
Difference: {3}
Exclusive Or: {3, 1, 2}
Cartesian Product: {(3,1), (3,4), (3,2), (3,5), (4,1), (4,4), (4,2), (4,5), (5,1),
(5,4), (5,2), (5,5)}
Do that again? [y/n] n
Goodbye!``````

If you post your honest own effort we help you out. Notice that even though experienced coder could do this code quickly, you should basically add code line be line making sure you get intended result from your additions by debug prints (or debug decorator). Quality is important as is readability of your code. Debugging time may well exceed the actual code writing time.

Small start anyway here:

``````input_set = '3 4 3 5 4'
print('Input: %s' % input_set)
set_list = []
for value in input_set.split():
if value not in set_list:
set_list.append(value)
print(set_list)``````

What i have so far, but i would only use this code in main if i didn't have to write the program as functions for the most part.. The function aspect of it is where i'm ultimately confused at.

``````def read_set0():
stored_elements = []
stored_set = input("Enter the elements of the set(separated with spaces): ").split()
stored_set = list(set(stored_set))
list(set(stored_set))
size=len(stored_set)
stored_elements.append(stored_set)
print("A set with", size, "elements has been read (repeating items were removed)")
return (stored_elements)``````

What this returns is:

``````read_set()
Enter the elements of the set(seperated with spaces): 4 5 3 4 2
A set with 4 elements has been read (repeating items were removed)
[['3', '2', '5', '4']]``````

I understood that you must make your own set, not using the built in set.

Isn't the project asking you to write a program where the user inputs his/her elements of their choice?

Where i've gotten so far(A little progress)... Question, since i have to ask the user to enter his/her elements twice, do i create the list(stored_set) outside of the function or is it better to keep it inside?

``````def read_set():
stored_set = input("Enter the elements of the set(separated with spaces): ").split()
stored_set=list(set(stored_set))
list(set(stored_set))
size = len(stored_set)
print ("A set with", size, "elements has been read (repeating items were removed)")
return

def print_set(A):

if __name__=="__main__":
print("\nEnter two sets, and i will print the result of some basic set operations on those sets.")

print(read_set())``````

Gives me a print out of:

``````Enter two sets, and i will print the result of some basic set operations on those sets.
Enter the elements of the set(separated with spaces): 5 4 6 5 4
A set with 3 elements has been read (repeating items were removed)
None``````

You should return the value of read set and save it in variable. Are you really allowed to use set ss uou are askrd not to save the values to list.

I'm really not sure what you're asking me.. Here's what i did though.

``````def read_set():
i = input("Enter the elements of the set(separated with spaces): ").split()
i=list(set(i))
list(set(i))
size = len(i)
print ("A set with", size, "elements has been read (repeating items were removed)")
return(i)
if __name__=="__main__":
print("\nEnter two sets, and i will print the result of some basic set operations on those sets.")
set_one = read_set()
set_two = read_set()``````
``````Enter two sets, and i will print the result of some basic set operations on those sets.
Enter the elements of the set(separated with spaces): 5 6 4
A set with 3 elements has been read (repeating items were removed)
Enter the elements of the set(separated with spaces): 2 5 3
A set with 3 elements has been read (repeating items were removed)
>>>``````

For some reason this message typing does not seem allways to function from this Nokia E7. Looks fine except line 4 which does nothing. I tried to write thet I do not understand the use of list to save values if you are allowed to use set like you used at line 3. Either you must use set for storage and operations or list and own functions, not both.

Thanks.. I believe i have it... I have a question.. In that 2nd function(print_set), i'm asked to:

Write a function called print set with a single parameter, a set A. The function prints the elements of the set using the usual set notation, i.e., surrounded by curly braces and separated by commas. For example, the set in the above example should be printed as
{2, 1, 3, 4}

Given my first function, you don't see anything that will present me with any problems do you?

``````def read_set():
i = input("Enter the elements of the set(separated with spaces): ").split()
i=list(set(i))
list(set(i))
size = len(i)
print ("A set with", size, "elements has been read (repeating items were removed)")
return(i)

def print_set(a):

if __name__=="__main__":
print("\nEnter two sets, and i will print the result of some basic set operations on those sets.")
set_one = read_set()
set_two = read_set()``````
``````Enter two sets, and i will print the result of some basic set operations on those sets.
Enter the elements of the set(separated with spaces):  6 6 4 5 2
A set with 4 elements has been read (repeating items were removed)
Enter the elements of the set(separated with spaces): 2 1 3 4
A set with 4 elements has been read (repeating items were removed)``````

well, from what I'm seeing it looks like it'll come up surrounded by brackets then curly braces.

``[{'my','set'}]``

because you made the set a list of the set, and I'm asking myself why you found that necessary... also, work on your names i is not a good name, I looked at that and was like; oh, are we playing with integers today? And then was terribly disappointed to find out otherwise :'(, lesson of the day, don't make me sad, and practice good practices.

You must write any statement as body of the function until you implement it, for example do nothing statement 'pass'. I do not understand why examples give numbers without quotes. We are taking strings as values, not only numbers.

well, from what I'm seeing it looks like it'll come up surrounded by brackets then curly braces.] because you made the set a list of the set, and I'm asking myself why you found that necessary... also, work on your names i is not a good name, I looked at that and was like; oh, are we playing with integers today? And then was terribly disappointed to find out otherwise :'(, lesson of the day, don't make me sad, and practice good practices.

How else could i find the length of each set?

I grant you a pass my fellow noob, here ya go, seeing is believing:

``````>>> this_set={'long','john','silver'}
>>>
>>> this_set
{'john', 'silver', 'long'}
>>>
>>>len(this_set)
3
>>>
>>> type(this_set)
<class 'set'>``````

You must check first that value does not exist in list before appending it to list.

I grant you a pass my fellow noob, here ya go, seeing is believing:

``````>>> this_set={'long','john','silver'}
>>>
>>> this_set
{'john', 'silver', 'long'}
>>>
>>>len(this_set)
3
>>>
>>> type(this_set)
<class 'set'>``````

This works better?

``````def read_set():
stored_set= input("Enter the elements of the set(separated with spaces): ").split()
size = len(stored_set)
print ("A set with", size, "elements has been read (repeating items were removed)")
return(stored_set)``````

and to remind you again that you don't need to stick the set in a list I bring you another presentation by the pyguy62 IDLE theater company:

``````>>> this_set
{'john', 'silver', 'long'}
>>>
>>>
>>> a
['long', 'john', 'silver']
>>>
>>>
>>> a.append('bob')
>>>
>>> for word in a:
this_set.add(word)

>>> this_set
{'bob', 'john', 'silver', 'long'}``````

A problem with the syntax that i presented though is that it doesn't remove the repeated items

the set will automatically remove items as they go in if they are pre-existing. Look closely at my last post.

the set will automatically remove items as they go in if they are pre-existing. Look closely at my last post.

I understand how to create a set, but what is the syntax for creating one from the users input?

Well

``````>>> stored_set= input("Enter the elements of the set(separated with spaces): ").split()
Enter the elements of the set(separated with spaces): my name is joshua
>>> stored_set=set(stored_set)
>>> stored_set
{'joshua', 'is', 'my', 'name'}
>>> len(stored_set)
4
>>>
>>> stored_set= input("Enter the elements of the set(separated with spaces): ").split()
Enter the elements of the set(separated with spaces): my name is joshua joshua
>>> stored_set=set(stored_set)
>>> stored_set
{'joshua', 'is', 'my', 'name'}
>>> len(stored_set)
4``````

is one option, but the EVEN MORE intuitive way is:

``````>>> stored_set= set(input("Enter the elements of the set(separated with spaces): ").split())
Enter the elements of the set(separated with spaces): joshua joshua bob
>>> stored_set
{'joshua', 'bob'}``````

just remember, Python is VERY much like English, say what you're trying to do out loud and there's a good chance you're very close like in this case "I need the input to be in a set" and essentially it came out to set(input()) which looks alot to me like input, in a set lol ;) You're in good hands here at Daniweb buddy, just keep puttin' in effort.

lol thank you and i can defintiely see that i'm in good hands here..

The print_set function, do you think it calls for some kind of loop, so that each element in stored_set can be printed into some kind of string?

Nope, looks to me like it's just straight up printing the set as-is

Nope, looks to me like it's just straight up printing the set as-is

But the set would be printed as {'3', '4', '5'} instead of being printed in the {3, 4, 5} form that it askes

Where i've gotten thus far.. There's gotta be away to remove the " ' " marks right?

``````def read_set():
stored_set= set(input("Enter the elements of the set(separated with spaces): ").split())
size = len(stored_set)
print ("A set with", size, "elements has been read (repeating items were removed)")
return(stored_set)

def print_set(a):
print (a)
return a

if __name__=="__main__":
print("\nEnter two sets, and i will print the result of some basic set operations on those sets.\n")
set_one = read_set()
set_two = read_set()
print("The two sets you entered are as follows: ")
print_set(set_one)
print_set(set_two)``````
``````Enter two sets, and i will print the result of some basic set operations on those sets.

Enter the elements of the set(separated with spaces): 4 5 3
A set with 3 elements has been read (repeating items were removed)
Enter the elements of the set(separated with spaces): 2 3 5 7
A set with 4 elements has been read (repeating items were removed)
The two sets you entered are as follows:
{'3', '5', '4'}
{'3', '2', '5', '7'}``````

You definately need sure answer if you need to do your own (inefficient) list based implementation of sets, you must do str instead of repr for elements (like when joined by join method), additionally result from cartesian product must be list of strings even they look like tuples (see my earlier comment of sanity of this). I did implement the full request with lists and checking with in like pyguy62 showed. Those set operations I did by list comprehensions and using each other (they are only single return statements).

Well if you're sure that you're going to be working with only integers then you could convert the elements in the set to integers 'because they are strings when inputed by the user thus the ' '. I did something like:

``````def print_set(some_set):
fixed_set=set()
for element in some_set:
try:
element=int(element)
fixed_set.add(element)
except ValueError:
print('\n',element,'was not an integer and will not be includeded in the final set\n')
some_set=fixed_set
return(some_set)``````

Thank You... do you mind going over my code and tellig me if i'm heading in the right direction or not, and if i'm not, tell me where i'm messing up at?? In my Union_set function i already know that when i try and implement it into my main it's not going to allow me because my Set_one and Set_two are list instead of sets. Is there anyway to fix that?

``````def read_set():
stored_set= set(input("Enter the elements of the set(separated with spaces): ").split())
size = len(stored_set)
print ("A set with", size, "elements has been read (repeating items were removed)")
return(stored_set)

def print_set(A):
fixed_set=set()
for element in A:
try:
element=int(element)
fixed_set.add(element)
except ValueError:
print('\n',element,'was not an integer and will not be includeded in the final set\n')
a=fixed_set
print (A)
return A

def union_set(A,B):
union= (A|B))
print(union)
return union

if __name__=="__main__":
print("\nEnter two sets, and i will print the result of some basic set operations on those sets.")
set_one = read_set()
set_two = read_set()

print("\nThe two sets you entered are as follows: \n")

print_set(set_one)
print_set(set_two)

union_set(set_one, set_two)``````

Btw, after everything is done, directions for my last function is :

A function called test set operations without any parameters. This function first asks the user to enter two sets. The function then prints the union, the intersection, the difference, the exclusive or, and the cartesian product of those two sets. All this should be done via calls to functions written in items 1-7 above. Reuse code! Do not repeat code that can be carried out by a function call instead. Points will be deducted for not following this fundamental programming principle. Also, allow the user to enter sets repeatedly.

You definately need sure answer if you need to do your own (inefficient) list based implementation of sets, you must do str instead of repr for elements (like when joined by join method), additionally result from cartesian product must be list of strings even they look like tuples (see my earlier comment of sanity of this). I did implement the full request with lists and checking with in like pyguy62 showed. Those set operations I did by list comprehensions and using each other (they are only single return statements).

What does repr stand for?

You are still using set at line 3. Use fundamental set definitions. For example union contains all items in first set and items in difference of second and first set.

EDIT: After your edit you are still using set, but now at line 2. Look pyguy's post where he showd how to append items in list only if they do not exist there allready. If you make it separate help function, say make_set(items), it would be better. Then you only add make_ in front of your set and write the definition for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.