Note that there are some explanatory texts on larger screens.

plurals
  1. POPython-Modulus-Stuck with a coin-for-given-dollar-amount scenario
    text
    copied!<p><strong>Specs:</strong> Ubuntu 13.04, Python 3.3.1</p> <p><strong>General Background:</strong> total beginner to Python; </p> <p><strong>Question-specific background:</strong> I'm exhausted trying to solve this problem, and I'm aware that, besides its instructional value for learning Python, this problem is boring and does not in any way make this world a better place :-( So I'd be even more grateful if you could share some guidance on this exhausting problem. But really don't want to waste your time if you are not interested in this kind of problems. </p> <p><strong>What I intended to do:</strong> "Calculate the number of basic American coins given a value less than 1 dollar. A penny is worth 1 cent, a nickel is worth 5 cents, a dime is worth 10 cents, and a quarter is worth 25 cents. It takes 100 cents to make 1 dollar. So given an amount less than 1 dollar (if using floats, convert to integers for this exercise), calculate the number of each type of coin necessary to achieve the amount, maximizing the number of larger denomination coins. For example, given $0.76, or 76 cents, the correct output would be "3 quarters and 1 penny." Output such as "76 pennies" and "2 quarters, 2 dimes, 1 nickel, and 1 penny" are not acceptable."</p> <p><strong>What I was able to come up with:</strong> </p> <pre><code>penny = 1 nickel = 5 dime = 10 quarter = 25 i = input("Please enter an amount no more than 1 dollar(in cents): ") i = int(i) if i &gt; 100: print ("Please enter an amount equal or less than 100. ") elif i &gt;= quarter: quarter_n = i % quarter i = i - quarter * quarter_n if i &gt;= dime: dime_n = i % dime i = i - dime * dime_n if i &gt;= nickel: nickel_n = i % nickel i = i - nickel * nickel_n if i &gt;= penny: penny_n = i % penny print (quarter_n,"quarters,",dime_n,"dimes",nickel_n,"nickels",penny_n,"pennies") else: if i &gt;= penny: penny_n = i % penny print (quarter_n,"quarters,",dime_n,"dimes",penny_n,"pennies") else: if i &gt;= nickel: nickel_n = i % nickel i = i - nickel * nickel_n if i &gt;= penny: penny_n = i % penny print (quarter_n,"quarters,",nickel_n,"nickels",penny_n,"pennies") else: if i &gt;= penny: penny_n = i % penny print (quarter_n,"quarters,",penny_n,"pennies") else: if i &gt;= dime: dime_n = i % dime i = i - dime * dime_n if i &gt;= nickel: nickel_n = i % nickel i = i - nickel * nickel_n if i &gt;= penny: penny_n = i % penny print (dime_n,"dimes",nickel_n,"nickels",penny_n,"pennies") else: if i &gt;= penny: penny_n = i % penny print (dime_n,"dimes",penny_n,"pennies") else: if i &gt;= nickel: nickel_n = i % nickel i = i - nickel * nickel_n if i &gt;= penny: penny_n = i % penny print (nickel_n,"nickels",penny_n,"pennies") else: if i &gt;= penny: penny_n = i % penny print (penny_n,"pennies") </code></pre> <p>This solution, though the best I could come up with, does not work as expected when fed with actual input numbers. And I'm unable to figure out why. Besides, I know that even from sheer size of the code that something is wrong. I searched for similar questions but the closest I got was one that dealt with very difficult math which I couldn't understand.</p> <p><strong>My question:</strong> I know I can't ask for a complete solution because that's down to me to figure it out. I'll appreciate either a) general pointer on the correct line of thinking b) critiques to my current code/line of thinking so that I might be able to improve it. </p> <p>Thank you for taking the time, even just reading this! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload