+ 2

I'm having problem in fixing an error in the following code. Can you please give your time?

Any suggestions/improvements will be highly appreciated! https://code.sololearn.com/cabYDB9wAE1q/?ref=app

4th Mar 2019, 2:23 PM
Ashutosh Dash
Ashutosh Dash - avatar
12 Answers
+ 7
Sure! Here's the code with a couple of annotations. Let me know if something is still unclear def atm_output(amount): banknotes = [50, 100, 200, 500, 2000] # list of all banknotes sorted from lowest to highest num_banknotes = 0 # this variable will contain the number of banknotes we need while amount: # repeat the following loop as long as there is a remainder if (amount and not banknotes # if there's still an amount remaining but we're out of banknotes or amount < min(banknotes)): # or the remaining amount is smaller than the banknote with the lowest denomination, return None # we can't withdraw that amount. Return None d = banknotes.pop() # pick the banknote with the highest denomination remaining. In the first iteration, this will be 2000, in the second iteration 500 etc. while d <= amount: # while the value of the highest banknote is smaller or equal to the remaining amount, num_banknotes += 1 # take one of those banknotes (increase number of total banknotes by 1) and amount -= d # decrease the remaining amount accordingly return num_banknotes # return number of banknotes print(atm_output(int(input())))
5th Mar 2019, 1:44 PM
Anna
Anna - avatar
+ 3
while d <= amount: num_banknotes += 1 amount -= d Let's say amount is 2500. d is the highest banknote (2000) d is <= amount, so you pick one $2000 bill and the amount is reduced by 2000. Now the remaining amount is 500 and the condition d <= amount is false. So you pick the next highest banknote which is 500 (this happens in the line d = banknotes.pop()) and the same routine starts over again.
7th Mar 2019, 5:08 AM
Anna
Anna - avatar
+ 3
What kind of error? /Edit: Make sure that all indentations are correct. Sometimes when I try to copy a code from Sololearn to a text editor or an IDE, all line breaks and indentations are removed: # original code def test(): print('yay') pass # copied code def test(): print('yay') pass So the code needs to be "repaired" first before you can run it.
8th Mar 2019, 6:23 AM
Anna
Anna - avatar
+ 2
Your r1, r2, ... r5 variable assignments happen within your previous if statements. If the if is wrong, then the r3 (for example) doesn’t get created, so it throws an error when it appears in the next if statement. Suggestion: keep modifying your wdraw variable: wdraw = wdraw%2000 etc...
4th Mar 2019, 2:40 PM
Russ
Russ - avatar
+ 2
The reason for getting error is that you declare and initiallize the rN variables n if blocks. But when the program is running those blocks are not read if the if statement returns false. Therefore no assignment for the rN variables. To get rid you have to declare all your variables at the top of the function wdraw = int(input('Enter Your Withdraw Amount:
#x27;)) r1 = wdraw%2000 r2 = r1 % 500 r3 = r2 % 200 r4 = r2 % 100 r5 = r4 % 50 EDIT: This will work as I have tested
4th Mar 2019, 2:43 PM
Seniru
Seniru - avatar
+ 2
Ashutosh Dash idk i just copied your code to the top. According to the sequence it should be BTW
4th Mar 2019, 2:57 PM
Seniru
Seniru - avatar
+ 2
$1250 with three banknotes isn't possible though or is there also a $1000 bill? https://code.sololearn.com/ca410BIoHFF4/?ref=app
4th Mar 2019, 3:59 PM
Anna
Anna - avatar
+ 1
Seniru Pasan are you sure r4 = r2...... or will it be r4=r3.....!!?
4th Mar 2019, 2:54 PM
Ashutosh Dash
Ashutosh Dash - avatar
+ 1
Yes it should be r4 = r3%... You made a typo in your code.
4th Mar 2019, 3:02 PM
Russ
Russ - avatar
+ 1
Anna thank you for your effort, Just a request, can you explain your code, using comments will be very helpful Edit: Anna I just improved the code, have a look!!!
5th Mar 2019, 1:31 PM
Ashutosh Dash
Ashutosh Dash - avatar
+ 1
Anna thanksfor your effort, but I'm unable to conclude a logic from the line 12
6th Mar 2019, 3:42 PM
Ashutosh Dash
Ashutosh Dash - avatar
0
Anna thanks for your response I want to draw your attention to the fact that when i copy/paste your code in VS Code, it's showing an error.
8th Mar 2019, 5:35 AM
Ashutosh Dash
Ashutosh Dash - avatar