Python Forum
I am having trouble understanding homework.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am having trouble understanding homework.
#1
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' }
buran write Feb-17-2021, 09:35 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
So show us the code you put in the spot where "your code goes here"
Reply
#3
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?
Reply
#4
okey Shy
Reply
#5
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'}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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)
Reply
#7
(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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020