Python Forum
I am having trouble understanding homework. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: I am having trouble understanding homework. (/thread-32538.html)



I am having trouble understanding homework. - theyhateadam - Feb-17-2021

Hello, I am currently stuck on a homework lab for zybooks and I am clueless as to what I am supposed to add to my code to make it work.
Prompt:
The top 3 most popular male names of 2017 are Oliver, Declan, and Henry according to babynames.com.

Write a program that modifies the male_names set by removing a name and adding a different name.
Sample output with inputs: 'Oliver' 'Atlas'

{ 'Atlas', 'Declan', 'Henry' }
NOTE: Because sets are unordered, the order in which the names in male_names appear may differ from above.

My code so far:
male_names = { 'Oliver', 'Declan', 'Henry' }
name_to_remove = input()
name_to_add = input()

''' Your solution goes here '''
male_names.remove('Oliver')
male_names.add('Atlas')

print(male_names)
I tried to remove the first name, and add a new one so far but I notice the expected output prints the names in a different order, some hints in the right direction would help.

Output:
Testing with inputs: 'Oliver' 'Atlas' Your output { 'Atlas', 'Declan', 'Henry' } Testing with inputs: 'Declan' 'Noah' Output differs. See highlights below. Your output { 'Atlas', 'Declan', 'Henry' } Expected output { 'Henry', 'Noah', 'Oliver' }



RE: I am having trouble understanding homework. - jefsummers - Feb-17-2021

So show us the code you put in the spot where "your code goes here"


RE: I am having trouble understanding homework. - BashBedlam - Feb-17-2021

What you need to remove is what is in the variable that you input from the user as the name to be removed which might not always be Oliver. The same goes for the name that needs to be added. Get it?


RE: I am having trouble understanding homework. - AntonySilva - Feb-20-2021

okey Shy


RE: I am having trouble understanding homework. - perfringo - Feb-20-2021

Sets are actually fun to work with. This is for example is symmetric_difference:

names = {'Oliver', 'Declan', 'Henry'}

for sample in ({'Oliver', 'Atlas'}, {'Declan', 'Noah'}):
    print(sample.symmetric_difference(names))
Output:
{'Declan', 'Henry', 'Atlas'} {'Oliver', 'Noah', 'Henry'}



RE: I am having trouble understanding homework. - Shaunayvette82 - Mar-02-2022

hey I realized late that there is more than one way to make the string to the new inputs

Try this

male_names = { 'Oliver', 'Declan', 'Henry' }
name_to_remove = input()
name_to_add = input()

male_names.remove(name_to_remove)

male_names.add(name_to_add)

print(male_names)


RE: I am having trouble understanding homework. - DeaD_EyE - Mar-02-2022

(Feb-17-2021, 01:09 AM)theyhateadam Wrote: male_names = { 'Oliver', 'Declan', 'Henry' }

This is a set literal. A set has only unique elements, and it does not keep any order.
For example, I get this order back: {'Declan', 'Henry', 'Oliver'}

If you want to get an alphanumerical order, you can use sorted on the set.
sorted_unique_names = sorted(male_name)
If the insertion order is important for you, then don't use a set. Instead, use a list.
You can't use a tuple because a tuple is immutable, and you can not add or remove elements from a tuple.

Since Python 3.6 dicts also keep the insertion order of keys and since Python 3.7 it's a standard of the Python language. Sometimes this feature is used, to get a set-like object with keeping the order.