patternpythonMinor
Coin Estimator by Weight
Viewed 0 times
coinestimatorweight
Problem
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 userChoos
```
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 userChoos
Solution
You got the right approach by using dictionaries and helper function (
Furthermore, having informations split in several collections, such as your
I would use a list to store these coin informations as it allows for automatic building of the menu:
Be aware, also, that your
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.Code Snippets
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)')Context
StackExchange Code Review Q#148240, answer score: 2
Revisions (0)
No revisions yet.