-2

How do I create a function that says that for example if a person spends 100 dollars they are bronze, whereas if they spend between 100 dollars and 200 dollars they are silver?

I am trying to learn how to use user defined functions rather than loops, so would prefer no suggestions using loops.

Thanks

Erika G
  • 11
  • welcome to SO. [Please read this to understand the MVCE](https://stackoverflow.com/help/mcve). Show what you have attempted, then come back and ask a specific question. – ivan7707 Nov 15 '17 at 21:43
  • Not using loops is a strange request: using a function and using loops aren't mutually exclusive. Probably most functions I write have some sort of loop in them. But, I suggest that you try to implement something yourself, and report the *specific difficulties* you are experience, otherwise, this question is going to be too broad for stack-overflow. – juanpa.arrivillaga Nov 15 '17 at 21:45

2 Answers2

0

Create a dictionary which maps the different increment keys to a value. Then you just need to calculate which key your 'amount_spent' is closest to and choose the value from the dictionary which this corresponds to

def prestige_level(amount_spent):
    levels = {0: 'bronze', 
              100: 'silver', 
              200: 'gold'}

    key = min([x for x in levels if amount_spent >= x], key=lambda x: abs(x - amount_spent))
    return levels[key]


print(prestige_level(1))
>> bronze
print(prestige_level(100))
>> silver
print(prestige_level(101))
>> silver
print(prestige_level(199))
>> silver
print(prestige_level(200))
>> gold
print(prestige_level(201))
>> gold
print(prestige_level(1000))
>> gold
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
  • Um, what's the point of using a `dict` if *you always have to iterate over it?* Why not `if, elif, else`? – juanpa.arrivillaga Nov 15 '17 at 21:56
  • I tried to use if and elif in my iteration but it doesn't work... do you think I should share that code and that could help me? – Erika G Nov 15 '17 at 21:58
  • Because the answer he's looking for is so simple it's not worth the effort to code. This however was a bit of a challenge, I enjoyed it and it's an answer to his question (plus its saved in my history for future reference) – Alan Kavanagh Nov 15 '17 at 21:58
  • @ErikaG if you want an answer on SO you should always share code. Otherwise you will be downvoted into oblivion – Alan Kavanagh Nov 15 '17 at 22:02
0

What you'd likely want to use here is an If Statement with comparative operators.

You could make a python function to do this like so.

def findSpendingLevel(moneySpent):
  if (moneySpent <= 100):
    return 'bronze'

  if (moneySpent > 100 and moneySpent <= 200):
    return 'silver'

  if (moneySpent > 200 and moneySpent <= 300):
    return 'gold'

  if (moneySpent > 300):
    return 'diamond'

Would likely be good for your purposes. Notice how this works by taking in an argument which is the moneySpent variable, and comparing it to the boundaries of your money levels with less-than-or-equal-to (<=) and greater-than (>) comparative operators.

Da Monster
  • 13
  • 4
  • I tried something very similarly to this originally, and at the end I said else : level ' does not exist' and then I said return level, but it didn't work. – Erika G Nov 15 '17 at 22:07
  • What was the code that you wrote? Can you share it in the question? – Da Monster Nov 15 '17 at 22:12