Python Forum
Adding and Removing coins to match Coin Bag Total infinite times
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding and Removing coins to match Coin Bag Total infinite times
#1
So I'm trying to make this program where if a user inputs their name, type of coin and weight of coin bag (which you put the coins in which is only LIMITED to a certain amount of coins), it will give or display the amount of coins which needed to be added or removed if the input of weight is too high or low. I'm also trying to make it if the bag isn't filled up, more coins will be added, the bag has to be full and also CAN NOT contain any other TYPE of coin. For example, a 1 Pence coin bag can only have 1 Pence coins in it.

I have managed to do this already, HOWEVER, the amount of repetitive code I would use is insane and it has its limits, like if the user has inputted more than 107* more pennies, I would have to write all of the same repetitive code up to 107. I'm looking for a way to make this algorithm work without using more than 50 lines of code FOR each coin.

Please provide a solution below as I can not find any way how to do this and explain how it works, I'm very new to python.

Please note, that it is IMPOSSIBLE for the user to add any other type of coin to a bag made for another coin. For example, if the user inputs the coin type of '1 Pence', and the Bag Weight is equals to a 1PenceFullBag + 2PenceCoin, it would just print 'unrecognized type of coin added to bag'. I don't need the program that specific.

I'm using Python2.x and I have thought about using loops but I'm not sure how to enforce it.

My main aim is to make it so my program will work out how many coins of that specific chosen type needed to be added or removed for it to equal to the bag weight of the chosen inputted coin, for an unlimited amount of times. I just can't figure it out.



one_pence_weight = 3.56
two_pence_weight = 7.12
five_pence_weight = 3.25
ten_pence_weight = 6.50             #These values are the weight of one coin.
twenty_pence_weight = 5.00
fifty_pence_weight = 8.00
one_pound_weight = 8.75
two_pound_weight = 12.00

one_pence_bag = 356.00
two_pence_bag = 360.50
five_pence_bag = 325.00
ten_pence_bag = 325.00               ##These values are the weight of a full coin bag.
twenty_pence_bag = 250.00
fifty_pence_bag = 160.00
one_pound_bag = 175.00
two_pound_bag = 120.00





##This code below is what I'm trying to achieve, but as you can see, 
##it would take me forever to do this for each and every single coin and 
##isn't an infinite amount of adding / removing

def verify(final_coin, final_weight):
    if final_coin == "1 Pence" and final_weight == one_pence_bag:
        print("Bag Correct and has no added or removed coins")
    if final_coin == "1 Pence" and final_weight == one_pence_bag - one_pence_weight:
        print("Bag incorrect and has added one 1p coin")
    if final_coin == "1 Pound" and final_weight == one_pound_bag - one_pound_weight:
        print("Bag inorrect and has added one 1Pound coin")
    else:
        pass

verify(coin_user_input, coin_weight_input)
Reply
#2
your problem starts from using different variable names for each coin or bag. Use some sort of data container, e.g. dict of dict or dict of tuples.
then write a generic function that based on user input AND respective values for coin and bag (from the dict) make necessary calculations. It will be more or less like what already did, but instead of variables for specific bag and coin you will take these values from the dict.
so basically you will have single data structure and single generic function to check the bag. Of course if you prefer you may have separate dict for coins and for bags, but one is sufficient
try to implement what i suggest and ask again if you face difficulty
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This sounds like it would work, but how would I write the values in a dict format?

would it look like this?

money_bags = {'one_pence_bag' : 356.00, 'two_pence_bag' : 360.50, 'five_pence_bag' : 325.00, 'ten_pence_bag' : 325.00, 'twenty_pence_bag' : 250.00, 'fifty_pence_bag' : 250.00, 'one_pound_bag' : 175.00, 'two_pound_bag' : 120.00}

coins = {'one_pence_weight' : 3.56, 'two_pence_weight' : 7.12, 'five_pence_weight' : 3.25, 'ten_pence_weight' : 6.50, 'twenty_pence_weight' : 5.00, 'fifty_pence_weight' : 8.00, 'one_pound_weight' : 8.75, 'two_pound_weight' : 12.0}
Reply
#4
one dict for both coin and bag
weights = {'1 pence': {'coin':3.56, 'bag':356.00},
           '2 pence':{'coin':7.12, 'bag':360.50},
           '5 pence':{'coin':3.25, 'bag':325.00},
           '10 pence':{'coin':6.50, 'bag':325.00},
           '20 pence':{'coin':5.00, 'bag':250.00},
           '50 pence':{'coin':8.00, 'bag':160.00},
           '1 pound':{'coin':8.75, 'bag':175.00},
           '2 poind':{'coin':12.00, 'bag':120.00}}

what you did (two separate dicts) is also ok, but look at the keys - you want the keys to be what user will enter as bag type
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Oh yes I see. Would I then use it in a for loop or while loop. I'm very unsure how I would use these said values in any kind of loop, though I do have some idea.
Reply
#6
(Sep-09-2018, 03:25 PM)Strayfe Wrote: Would I then use it in a for loop or while loop
You don't need loop in the function that checks specific bag. note that you already know what type of bag it is. Then you can extract the value from the dict or dicts(if you stick to two dict approach) just using the key.
If you are not sure how to access dict values, check our tutorial
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Oh no I understand how the dictionary works but I'm wondering how I would use the dictionary with my function 'verify'. I can't figure out how to add or remove certain amount of coins based on the user inputted weight of bag. For example, if the user selected from a dropdown menu using tkinter (in my case) or inputted the coin they was using such as '1 pence', but the weight of bag (which they also input) was higher than the limit '356.00', it would print out how many 1p coins needed to be removed. As said, I'm not sure whether to use a for loop for or something else, I really just can't think of how to enforce this in to a function.
Reply
#8
1. put the dict inside the function, so you could access it
2. your function will take two arguments - type of coin (that is what you call final_coin, but the name is not very correct, right?) and current weight of bag
3.use the type of coin that user provides and you pass as argument to extract the value for one coin and for a [standard] bag of coins from the dict. Again, you don't need a loop.
4.make the calculations and return the result
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
You might want to use integers rather than floats as you will have problem with accuracy on comparing weights later.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print the frequency of each coin for the combinations that sum to the amount N Pranav 3 2,526 May-19-2020, 06:16 AM
Last Post: Pranav
  Random module, coin flipping gus17 3 4,831 Jan-06-2020, 10:29 AM
Last Post: perfringo
  Need help with coin change program Gateux 2 6,020 Jun-25-2019, 02:32 PM
Last Post: perfringo
  Find how many times a user played an artist and how many times disruptfwd8 1 2,583 May-04-2018, 08:32 AM
Last Post: killerrex
  Match coins game AndJustice4A11x 1 5,975 Nov-05-2017, 01:43 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020