2

I am new to coding and I'm doing an assignment for school where we have to write a program that asks how many people in your family you are shopping for during the holidays and how much you are going to spend on each one. We are also defining a favorite, so the person you are spending the most on is the favorite.

So far I've written the code so that it asks for the number of people you are shopping for, the amount you are spending on each, and then it defines a favorite. The part that I'm stuck with is if there is no maximum amount; for example, if you are spending $100 on more than one person, or you are spending the same amount for everyone.

This is what I have so far

def family_names():
  print("It's holiday season! \n")
  names = []
  n = int(input("How many family members are you shopping for this year? "))
  print()
  for i in range(n):
    names.append(input("Enter your first family member's name: ").title())
  return names

def spend(family):
  print()
  amount = []
  for i in family:
    print("How much will you spend on ", i, "? ", sep ="", end ="")
    amount.append(int(input()))
  return amount

def favorite(family, amount):
  price = amount.index(max(amount))
  fav = family[price]
  print(fav, " is your favorite family member because you spent $", max(amount), " on them.", sep = "")



#-----main-----
people = family_names()
money = spend(people)
favorite(people,money)```

JustGit
  • 23
  • 3
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – ChrisGPT was on strike Jun 15 '20 at 22:39
  • Does your assignment have a specification for the intended behavior regarding ties? If not, you probably need to seek clarification. – Fred Larson Jun 15 '20 at 22:39
  • No, the assignment does not specify what to do with ties, it just says to have the program display the favorite. I just realized while I was testing that if I entered the same amount for more than one person it just chooses the first person as the favorite. I wanted to add it just for fun. – JustGit Jun 15 '20 at 22:59
  • "Does this answer your question? Asking the user for input until they give a valid response – Chris " I don't think so. The person who posted that was trying to get the input to repeat if an invalid input was given. I already have my input correct, I just want the program to recognize if more than one of the inputs are the same. – JustGit Jun 15 '20 at 23:21

1 Answers1

1

Here is what you can do:

def favorite(family, amount):
  for f,a in zip(family,amount):
    if a==max(amount):
       print(f, " is your favorite family member because you spent $", max(amount), " on them.", sep = "")

This function will return the print out member as long as the amount spent on them equals to the max(amount).


UPDATE

Updated to include no favorite option:

def favorite(family, amount):
  m = [a for a in amount if a != max(amount)]
  if len(amount)-len(m)>1:
      print("There is no favorite.")
  else:
    for f,a in zip(family,amount):
      if a==max(amount):
         print(f, " is your favorite family member because you spent $", a, " on them.", sep = "")

Or

def favorite(family, amount):
  m = [a for a in amount if a != max(amount)]
  if len(amount)-len(m)>1:
      print("There is no favorite.")
  else:
      print(family[amount.index(max(amount))], " is your favorite family member because you spent $", max(amount), " on them.", sep = "")
Red
  • 26,798
  • 7
  • 36
  • 58
  • This is better. Now if there is a tie for the max amount it prints a statement for each. Thank you. – JustGit Jun 16 '20 at 00:30
  • I don't quite understand, do you still need help? – Red Jun 16 '20 at 00:31
  • I was hoping there was a way to use an if statement to say something like, if there is no max or the max amount occurs more than once, there is no favorite. – JustGit Jun 16 '20 at 00:39
  • 1
    Awesome, that works exactly the way I wanted. Thank you so much :) – JustGit Jun 16 '20 at 01:04