2

A organisation classifies the amounts donated by the public into two different tiers:

  • Tier 1: Amounts greater than or equal to $100
  • Tier 2: Amounts less than $100

Write a program that can ask for the total number of donations received, as well as the donated amounts. After which, display the following for each tier:

  1. List of donated amounts
  2. Total amount
  3. Average amount

After the user has input the total number of donors, the while loop will print "Enter amount donated by donor (number)" until it reaches the last donor. I have no idea how to go about doing that. This is my current attempt:

donors=int(input("Enter total number of donations received: "))
tier1=[]
tier2=[]
i=0
while donors < (donors+1):
    amount=int(input("Enter amount donated by donor {0}: ".format(i+1)))
    if amount >=100:
        tier1.append(amount)
    else:
        tier2.append(amount)
average1=(sum(tier1)/len(tier1))
average2=(sum(tier2)/len(tier2))
print("Tier 1 donations received is " +str(tier1))
print("Total amount for Tier 1 is {0}".format(sum(tier1)))
print("Average amount for Tier 1 is $" + str(average1))
print("Tier 1 donations received is " + str(tier2))
print("Total amount for Tier 1 is {0}".format(sum(tier2)))
print("Average amount for Tier 1 is $" + str(average2))

The output keeps printing "Enter amount donated by donor 1:" instead of

"Enter amount donated by donor 1: "
"Enter amount donated by donor 2: "
"Enter amount donated by donor 3: "

1 Answer 1

1

Your loop will run infinitely, since donors < (donors + 1) always evaluates to True. Perhaps you meant to say while i < donors, in order to loop from i=0 at the start to when i == donors, at which point it will stop. For this to work, you have to increment the value of i in your loop though:

i=0
while i < donors:
    i += 1  # Increment your counter here
    amount=int(input("Enter amount donated by donor {0}: ".format(i)))
    if amount >=100:
        tier1.append(amount)
    else:
        tier2.append(amount)

A better and more pythonic method is to use range and a for loop for this instead of while. Replace the following:

i=0
while donors < (donors+1):

With:

for i in range(donors):
Sign up to request clarification or add additional context in comments.

1 Comment

Ah so for i in range needs to be used instead of while... have been grinding my gears around while and for loops without thinking of the range function... Thank you for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.