5
\$\begingroup\$

I've created a mini project where the user will enter the total weight of the coins and the program will compute how many coins they have and how many wrappers they'll need. I initially found this challenge in a subreddit named r/beginnerprojects

def coinEstimator():

    ##########################################
    # Intro text                        ######
    ##########################################

    print("Welcome to the coin estimator.")
    print("Please enter the type of coins: ")
    print("1. Cent")
    print("2. Nickel")
    print("3. Dime")
    print("4. Quarter")


    ##############################################
    # Setting up variables for the user's input ##
    ##############################################

    userChoose = int(input("Enter: "))


    ##############################################################
    # Conditionals for different types of coin the user wants.  ##
    ##############################################################

    if userChoose == 1:             # Conditionals for Cents
        totalWeight = float(input("What is the total weight of your cents in grams?\n"))

        (cointValue, dimeWrapper) = cointEstSol(totalWeight, "Cent")

        print("The total cents you have is {0}. You need {1} wrapper(s)".format(cointValue, dimeWrapper))

    elif userChoose == 2:           # Conditionals for Nickel
        totalWeight = float(input("What is the total weight of your nickels in grams?\n"))

        (cointValue, dimeWrapper) = cointEstSol(totalWeight, "Nickel")

        print("The total nickels you have is {0}. You need {1} wrapper(s)".format(cointValue, dimeWrapper))

    elif userChoose == 3:           # Conditionals for Dime
        totalWeight = float(input("What is the total weight of your dimes in grams?\n"))

        (cointValue, dimeWrapper) = cointEstSol(totalWeight, "Dime")

        print("The total dimes you have is {0}. You need {1} wrapper(s)".format(cointValue, dimeWrapper))

    elif userChoose == 4:           # Conditionals for Quarter
        totalWeight = float(input("What is the total weight of your quarters in grams?\n"))

        (cointValue, dimeWrapper) = cointEstSol(totalWeight, "Quarter")

        print("The total quarters you have is {0}. You need {1} wrapper(s)".format(cointValue, dimeWrapper))


def cointEstSol(weight, coins):

    ########################################################
    ## Setting up variables for the solution of coins    ###
    ########################################################

    coinWeight = {"Cent": 126, "Nickel": 199, "Dime": 113, "Quarter": 226}     # Dictionary for the different weights of coins
    coinCountWrap = {"Cent": 50, "Nickel": 40, "Dime": 50, "Quarter": 40}      # Dictionary for the amount of coins that can be put in a wrapper

    cointValue = weight // coinWeight[coins]                                   # Solution to compute how many coins we have

    if cointValue < coinCountWrap[coins]:                                      # Conditionals for how many wrappers would be needed.
        coinWrapper = 1
    else:
        coinWrapper = cointValue // 50
        coinWrapper += 1

    return (cointValue, coinWrapper)                                           # Returning the total amount of coins and wrapper

coinEstimator()

Please tell me what areas i need to improve. As you can see my code is crude and was not beautifully written.

Honest review would be needed. Any shortcuts, codes that need amendments or just a way to improve this code would be appreciated.

Extra inquiry: Although i don't think this would be the appropriate place to ask this questions but i want this to be made as object oriented programming approach. I don't know how, so if you guys can convert this code to OOP, that would be great. I wanted to know how python experts would approach this in an OOP style. It's okay if you don't answer this.

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You got the right approach by using dictionaries and helper function (coinEstSol). You should go further and provide the same kind of approach to coinEstimator.

Furthermore, having informations split in several collections, such as your coinWeight and coinCountWrap is not ideal as it gets harder to extend them latter. Instead, you could work with triplets reprensenting a coin: name, weight and wrap. You could also use collections.namedtuple to make for better understanding of what these values are.

I would use a list to store these coin informations as it allows for automatic building of the menu:

import collections


Coin = collections.namedtuple('Coin', 'name weight wrap')


COINS = [
    Coin('Cent', 126, 50),
    Coin('Nickel', 199, 40),
    Coin('Dime', 113, 50),
    Coin('Quarter', 226, 40),
]


def estimate(coin):
     question = 'What is the total weight of your {}s in grams?\n'.format(coin.name)
     weight = float(input(question))
     value = weight // coin.weight
     wrapper = value // coin.wrap
     if value % coin.wrap:
         wrapper += 1
     return coin.name, value, wrapper


def main():
    print("Welcome to the coin estimator.")
    print("Please enter the type of coins: ")
    for index, coin in enumerate(COINS, 1):
        print(index, coin.name)

    user_choice = int(input('Enter: '))
    name, value, wrapper = estimate(COINS[user_choice - 1])
    print('The total', name, 'you have is', value, 'You need', wrapper, 'wrapper(s)')

Be aware, also, that your float and int calls can raise ValueError if the user doesn't enter a numeric value. You can also get an IndexError if the choice from the user is over the number of coins defined. Depending of your needs, you may want to take care of that.

\$\endgroup\$
1
  • \$\begingroup\$ I see, by using a triplets i could easily take care the management of the information. Wow your code looks beautiful. Its so clean. I 've learned alot. Thank you. \$\endgroup\$
    – Dimasalang
    Nov 28, 2016 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.