+ 1

How to make a program that inputs cash, writes it down and calculates the total?

So, I was counting coins and bills at my work and got an idea. I just want to fill in a list like this and automatically create a file for it: 5c: 22 10c: 17 20c: 11 50c: 18 1e: 18 2e: 24 5e: 2 10e: 0 20e: 3 50e: 3 Total: 300e I was about to make it, but instantly realized that my way is gonna be like 100+ lines that don't make any sense. So, help?

12th May 2022, 9:48 AM
Ville Nordström
Ville Nordström - avatar
8 Respostas
+ 3
Ville Nordström money = {0.05 : 22, 0.10 : 17, 0.20 : 11, 0.50 : 18, 1.00 : 18, 2.00 : 24, 5.00 : 2, 10.00 : 0, 20.00 : 3, 50.00 : 3, } total = 0 for i in money: total += i * money[i] print(total) https://code.sololearn.com/ckvz45M2BW7P/?ref=app
12th May 2022, 10:10 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Ville Nordström You could create a while loop that takes input until an exit condition has been met. if you had a dictionary with all the values set to 0, then you could assign the inputs to those dictionary values as they were entered. It would be a nice project for you. You will need to review while loops, dictionaries & variable reassignment. Be aware that multiple inputs using a while loop does not work well on Sololearn. All inputs must be entered before running the code. An external IDE would be more intuitive for this code. Good luck
12th May 2022, 10:38 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
Ville Nordström , i have put 2 versions in the file, also some comments. have a look how the input can be done: https://code.sololearn.com/cI0g1F2NE2GS/?ref=app
12th May 2022, 2:28 PM
Lothar
Lothar - avatar
+ 2
Ville Nordström When you are inputting the information, have you already counted the items? Your example looks like a dictionary, not a list
12th May 2022, 9:59 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Ville Nordström Your suggestion of a for loop is also valid, especially because there is a known number of inputs required
12th May 2022, 10:43 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 1
Ville Nordström dic = {.05: 0, .10: 0, .20: 0, .50: 0, 1.00: 0, 2.00: 0, 5.00: 0, 10.00: 0, 20.00: 0, 50.00: 0 } for i in dic: dic[i] = int(input()) print(dic) https://code.sololearn.com/cYia7djQZze2/?ref=app
12th May 2022, 10:51 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
Rik Wittkopp Oh wow, thank you! How would I make an input take those in one by one? I bet that could be done in a very compact way as well, but I can't think of a way. Because my idea is that there are 9 "questions", I could make a loop that cycles through that dictionary until the value is 9, but yeah.. not sure if that's the best way either.
12th May 2022, 10:27 AM
Ville Nordström
Ville Nordström - avatar
0
Thank you very much :) And yeah, I normally use Visual Studio and that has worked well for me in the past
12th May 2022, 10:39 AM
Ville Nordström
Ville Nordström - avatar