Checking if a bag has enough coins in program The objective of the program is to check that the weight of the bag that the user inputs and then calculate how many coins are needed to the added/removed to ensure the bag has the right amount of coins in. It needs to display the difference in weight and the number of coins to be added/removed and a total value of coins added/removed. The weights can be found within the file.

The bags for 1p and 2p add up to £1. The bags for 5p and 10p add up to £5. The bags for 20p and 50p add up to £10. The bags for 100p and 200p add up to £20.

print("Calculation")
print("===========================\n")

one_coin_weight = 3.56
one_bag_amount = 1 / 0.01
one_bag_weight = one_coin_weight * one_bag_amount

two_coin_weight = 7.12
two_bag_amount = 1 / 0.01
two_bag_weight = two_coin_weight * two_bag_amount

five_coin_weight = 3.25
five_bag_amount = 5 / 0.05
five_bag_weight = five_coin_weight * five_bag_amount

ten_coin_weight = 6.50
ten_bag_amount = 5 / 0.10
ten_bag_weight = ten_coin_weight * ten_bag_amount

twenty_coin_weight = 5.00
twenty_bag_amount = 10 / 0.20
twenty_bag_weight = twenty_coin_weight * twenty_bag_amount

fifty_coin_weight = 8.00
fifty_bag_amount = 10 / 0.50
fifty_bag_weight = fifty_coin_weight * fifty_bag_amount

onehundred_coin_weight = 9.50
onehundred_bag_amount = 20 / 1.00
onehundred_bag_weight = onehundred_coin_weight * onehundred_bag_amount

twohundred_coin_weight = 12.00
twohundred_bag_amount = 20 / 2.00
twohundred_bag_weight = twohundred_coin_weight * twohundred_bag_amount

coins_value_added = 0
coins_value_removed = 0

bag_count = int(input("How many bags do you have? "))
print("\n===========================\n")

for x in range(bag_count):
    print("Bag", x + 1, "\n")

    bag_weight = float(input("How much does the bag weigh in grams? "))
    coin_type = int(input("Which type of coin is in the bag in pence? "))

    if coin_type == 1:
        weight_difference = one_bag_weight - bag_weight
        coins_changed = weight_difference / one_coin_weight
    elif coin_type == 2:
        weight_difference = two_bag_weight - bag_weight
        coins_changed = weight_difference / two_coin_weight
    elif coin_type == 5:
        weight_difference = five_bag_weight - bag_weight
        coins_changed = weight_difference / five_coin_weight
    elif coin_type == 10:
        weight_difference = ten_bag_weight - bag_weight
        coins_changed = weight_difference / ten_coin_weight
    elif coin_type == 20:
        weight_difference = twenty_bag_weight - bag_weight
        coins_changed = weight_difference / twenty_coin_weight
    elif coin_type == 50:
        weight_difference = fifty_bag_weight - bag_weight
        coins_changed = weight_difference / fifty_coin_weight
    elif coin_type == 100:
        weight_difference = onehundred_bag_weight - bag_weight
        coins_changed = weight_difference / onehundred_coin_weight
    elif coin_type == 200:
        weight_difference = twohundred_bag_weight - bag_weight
        coins_changed = weight_difference / twohundred_coin_weight

    print("\nThe weight difference is", str(round(weight_difference)) + "g.")

    if coins_changed &gt; 0:
        print("Add", round(coins_changed), "coins to the bag.\n")
        coins_value_added += coin_type
    else:
        print("Remove", round(coins_changed), "coins from the bag.\n")
        coins_value_removed += coin_type

    print("\n===========================\n")

print("There were", bag_count, "bags checked.")
print("There is", coins_value_added, "pence that needs to be added.")
print("There is", coins_value_removed, "pence that needs to be removed.")
1) There's too much duplication. You can define a map, such as:

#This defines a mapping from coin value to a tuple (coin weight, monetary sack value)
coin_types = { 1: (3.56, 1), 2: (7.12, 1), ... }


Then, it's easy to calculate weights and amounts as such:

#This defines a mapping from coin value to a tuple (amount of coins in sack, sack weight)
coins_in_sack_by_type = {k: (v[1] / k, v[1] / k * v[2]) for (k, v) in coin_types.items()}


2) Now, most of your code in the main loop can be collapsed into:

weight_difference = coins_in_sack_by_type[coin_type][1] - sack_weight
coins_changed = weight_difference / coin_types[coin_type][0]