Home > OS >  How do I turn this into a for loop from range(0, 99) instead of user input?
How do I turn this into a for loop from range(0, 99) instead of user input?

Time:09-12

Help, How do I turn this into a for loop from range(0, 99) instead of user input?

amount = int(input("enter the amount in cents from 0 to 99: ")) 
cents = amount
quarters = amount // 25
amount = quarters % 25  
dimes = amount // 10  
amount = amount % 10  
nickels = amount // 5  
amount = amount % 5  
pennies = amount  
print(cents, quarters, dimes, nickels, pennies)

CodePudding user response:

I don't know if I understand right, but the solution is:

for amount in range(100):
    cents = amount
    quarters = amount // 25
    amount = quarters % 25  
    dimes = amount // 10  
    amount = amount % 10  
    nickels = amount // 5  
    amount = amount % 5  
    pennies = amount  
    print(cents, quarters, dimes, nickels, pennies)

The number in range() need to bee 100 because Python not consider the last number.

CodePudding user response:

Your current logic has a bug. If you try 99 as the input, the result will be 99 3 0 0 3 ie. 3 quarters and 3 pennies, which is NOT 99 cents.

You're doing a % on quarters instead of amount

If you fix that and then put it in a standard for loop, you'll get the following:

for amount in range(100):
    cents = amount
    quarters = amount // 25
    amount = amount % 25  
    dimes = amount // 10  
    amount = amount % 10  
    nickels = amount // 5  
    amount = amount % 5  
    pennies = amount  
    print(cents, quarters, dimes, nickels, pennies)

CodePudding user response:

Maybe that's what you've working and looking for? Try it and modify to suit your own requirement: (this is to follow your logic/syntax as much as it can...)

NB - as previous comment, you could simplify the math by using divmod.


for amount in range(25, 51):                 # just to limit the testing outputs
    cents = amount
    print('Returning', cents, 'cents as: ')

    quarters = cents // 25
    rest = cents - 25 * quarters
    dimes = rest// 10
    rest = rest - 10 *dimes
    nickles = rest// 5
    rest = rest - 5 *nickles
    pennies = rest
    print('%d of quarter = - cents' % (quarters, quarters*25))
    print('   %d of dime = - cents' % (dimes, dimes*10))
    print(' %d of nickle = - cents' % (nickles, nickles*5 ))
    print('  %d of penny = - cents' % (pennies, pennies))
    # checking...
   

Outputs: (just take first few here)

-------------------------
Returning 25 cents as: 
1 of quarter = 25 cents
   0 of dime =  0 cents
 0 of nickle =  0 cents
  0 of penny =  0 cents
Returning 26 cents as: 
1 of quarter = 25 cents
   0 of dime =  0 cents
 0 of nickle =  0 cents
  1 of penny =  1 cents

If amount is 98/99:

Returning 98 cents as: 
3 of quarter = 75 cents
   2 of dime = 20 cents
 0 of nickle =  0 cents
  3 of penny =  3 cents
Returning 99 cents as: 
3 of quarter = 75 cents
   2 of dime = 20 cents
 0 of nickle =  0 cents
  4 of penny =  4 cents
  • Related