denomination of coins in def main() python - python

I am writing a script that asks the user for the numbers of coins of different denominations that they have, then displays the total sum in dollars and cents.
I am stuck at this point and am confused on what exactly is wrong. This is part of my intro class, so detail to what needs to be changed is greatly appreciated. The output I need is to tell the user how many dollars and left over cents there are. I am having a problem now with the output. The function tells me the right answer but when wording the answer the cent(s) are off.
For example when pennies are 5; nickels 3; dimes 3; quarters 3, the total is 1.25 but the function says "You have 1 dollars and 1 cent(s)"
def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $",format(total_value, ".2f"), sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
get_input = int(input(message))
return get_input
def get_total(pennies, nickels, dimes, quarters):
pennies = (.01 * pennies);
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value = pennies + nickels + dimes + quarters
return total_value
def get_dollars(pennies, nickels, dimes, quarters):
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = format(total_value // 1, ".0f")
return dollars
def get_left_over_cents(pennies, nickels, dimes, quarters):
total_value = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = total_value
return left_over_cents
main()

The error actually lives in your get_input method.
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
get_input = int(input(message))
return get_input
which returns None if the original input is > 0. You might want to unindent the return clause once.

Related

Why is my input function not supported?

def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels: ")
dimes = get_input("Enter dimes: ")
quarters = get_input("Enter quarters: ")
print("You Entered : ")
print("\tPennies : " , pennies)
print("\tNickels :" , nickels)
print("\tDimes :" , dimes)
print("\tQuarters:" , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $",total_value, sep="")
print("You Have", dollars, "dollars and", left_over_cents, "cent(s)")
I keep getting an error that states TypeError: unsupported operand type(s) for *: 'float' and 'NoneType. I've browsed answers that relate to this error on this website but found nothing that was able to help me out. After the user inputs four numbers, the program give me the error after trying to execute the next part of the code.
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
return get_input
In this portion. If the user inputs a number 0 or lower, the while loop is supposed to return to the function but it just writes down 0 instead. I am sorry these are probably really easy solutions but I am in my intro class and this stuff is rather difficult for me.
def get_total(pennies, nickels, dimes, quarters):
pennies = (.01 * pennies);
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value = pennies + nickels + dimes + quarters
return total_value
def get_dollars(pennies, nickels, dimes, quarters):
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = format(total_value // 1, ".0f")
return dollars
def get_left_over_cents(pennies, nickels, dimes, quarters):
total_value = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = total_value
return left_over_cents
main()
Your problem is in your get_input function:
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
return get_input
You are only returning a value if you input something less than or equal to 0, apparently not desired input. Instead, you should move the return outside of the while statement, or consider improving it so that it only returns if the input is greater than 0:
def get_input(message):
get_input = -1
while get_input <= 0:
print("Enter a number greater than 0")
get_input = int(input(message))
return get_input
Such that:
>>> var = get_input('Enter: ')
Enter a number greater than 0
Enter: -4
Enter a number greater than 0
Enter: 0
Enter a number greater than 0
Enter: 5
>>> var
5
>>>
Earlier, you did not return anything so your value for pennies was stored as None. Python tried to multiply by pennies, None, hence your error:
>>> def get_input(message):
... get_input = int(input(message))
... while get_input <= 0:
... print("Error")
... return get_input
...
>>> pennies = get_input('Enter: ')
Enter: 9
>>> print(0.1*pennies)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
First of all, I think your input function is wrong:
def get_input(message):
get_input = int(input(message))
while get_input <= 0 :
print("Error")
return get_input
Shouldn't this be:
def get_input(message):
get_input = int(input(message))
while get_input <= 0:
print("Error")
get_input = int(input(message))
return get_input
Most (strong) compilers won't even compile this, because they will convert your program into traces, and notice there is a possibility that your program returns without giving any value.
Since Python is weakly typed, this is not an issue: if no return statement is presented, it simply returns nothing, but ends the method.
Thus when you enter valid data, your method doesn't return anything, thus the result can't be processed.
This is one of the advantages of strongly typed languages over weakly typed ones like Python, although I admit there are other arguments that favor Python over say Java.

How do I return a correctly rounded answer in Python?

I am writing a script that asks the user for the numbers of coins of different denominations that they have, then displays the total sum in dollars and cents.
When it displays dollars, however, it rounds the answer.
For example, if the total is 82 cents, it states that I have 1 dollar while I need it to say 0 dollars.
I can't make use of the round function appropriately here.
Is it even a smart thing to use or should I stick with the format function?
def main():
pennies = get_input( "Enter pennies : " )
nickels = get_input( "Enter nickels : " )
dimes = get_input( "Enter dimes : " )
quarters = get_input( "Enter quarters : " )
print( "You Entered : " )
print( "\tPennies : " , pennies )
print( "\tNickels : " , nickels )
print( "\tDimes : " , dimes )
print( "\tQuarters : " , quarters )
total_value = get_total( pennies, nickels, dimes, quarters )
dollars = get_dollars( pennies, nickels, dimes, quarters )
left_over_cents = get_left_over_cents( pennies, nickels, dimes, quarters )
print( "Total = $",format( total_value, ".2f" ), sep = "" )
print( "You Have", format( dollars, ".0f" ), "dollars and", left_over_cents, "cent(s)" )
def get_input( message ):
notify = int( input( "I don't know" ) )
while notify >= 0:
return notify
def get_total( pennies, nickels, dimes, quarters ):
pennies = .01 * pennies;
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value = pennies + nickels + dimes + quarters
return total_value
def get_dollars( pennies, nickels, dimes, quarters ):
pennies = .01 * pennies;
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value = pennies + nickels + dimes + quarters
dollars = total_value
return dollars
def get_left_over_cents( pennies, nickels, dimes, quarters ):
pennies = .01 * pennies;
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value = pennies + nickels + dimes + quarters
left_over_cents = total_value
return left_over_cents
main()
Typically best is to use decimal.Decimal for monetary values:
import decimal
penny_value = decimal.Decimal('.01')
nickle_value = decimal.Decimal('.05')
dime_value = decimal.Decimal('.10')
quarter_value = decimal.Decimal('.25')
If you're keeping count of coins, you should multiply the above constants by your number of coins, and you won't have rounding errors.
pennies = 2
nickles = 1
dimes = 0
quarters = 3
total_value = (pennies * penny_value + nickles * nickle_value +
dimes * dime_value + quarters * quarter_value)
dollars = total_value.quantize(1, rounding=decimal.ROUND_DOWN)
and dollars is now Decimal('0') which is equal to zero.
>>> print total_value
0.82
>>> print dollars
0
If you want to stick to ints and floats to round down, convert to int, which will round towards zero:
total_value = .82
dollar_value = int(total_value)
dollar_value is now 0
To use this approach, do this:
def get_dollars(pennies, nickels, dimes, quarters):
pennies = .01 * pennies;
nickels = .05 * nickels;
dimes = .10 * dimes;
quarters = .25 * quarters;
total_value= pennies + nickels + dimes + quarters
dollars = int(total_value)
return dollars

ValueError: invalid literal for int() with base 10 cannot figure out why

I cannot seem to figure out why when i run my program i receive the error ValueError: invalid literal for int() with base 10: 'Enter pennies : '.
The entire program was made by my instructor so we could add the functions to make it work. im currently trying to define get_input1 but im having no luck. any help would be great.
def main():
pennies = get_input1("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input1(pennies):
int(input("Enter Pennies: "))
if int(pennies) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of pennies: '))
main()
change this:
int(input("Enter Pennies: "))
to this:
pennies = input("Enter Pennies: ")
Edited
I believe this is just a typo, you should be assigning pennies to the input result.
def main():
pennies = get_input1("Enter pennies : ")
nickels = get_input2("Enter nickels : ")
dimes = get_input3("Enter dimes : ")
quarters = get_input4("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input1(pennies):
pennies = int(input("Enter Pennies: "))
if int(pennies) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of pennies: '))
return(pennies)
def get_input2(nickels):
nickels = int(input("Enter nickels: "))
if int(nickels) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of nickels: '))
return(nickels)
def get_input3(dimes):
dimes = int(input("Enter dimes: "))
if int(dimes) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of dimes: '))
return(dimes)
def get_input4(quarters):
quarters = int(input("Enter quarters: "))
if int(quarters) < 0:
print('Error: money cannot be negative')
pennies = int(input('Enter correct amount of quarters: '))
return(quarters)
main()
This will get you through defining your pennies,nickels,dimes and quarters. Now you will have to define get_total, get_dollar, and get_left_over_cents.
You needed to have pennies in front of the int value is why you were receiving the error. Then you can return that value of pennies to the main.
Hope this helps!
Your data flow is backwards. In main(), you're calling get_input1 to retrieve values, much as with input(), but if we look at that get_input1 does:
# Names its argument, which was a question, "pennies"
def get_input1(pennies):
# Asks a question, converts to int, and discards it
int(input("Enter Pennies: "))
if int(pennies) < 0: # Converts pennies to int; but it was a question!
print('Error: money cannot be negative')
# This is the only place to alter pennies, but it doesn't return it
pennies = int(input('Enter correct amount of pennies: '))
It looks like this used to be code that was refactored to handle the various coins with a function call each, but it doesn't have the two modifications it needs: a way to alter what it asks for, and a way to return what the answer was. Note that function arguments, like pennies in get_input1, are local variables; main never sees it changing.

Need help defining a function in python

I need to code a get_input function that has loop validation so the number cannot be less than 0
This is the program. ive made this a global function but my instructor told me it was wrong. Making get_input a global function seems to work but i need to use the means of
def get_input():
get_input()
Ive been setting up the global function as get_input = input just because i have no clue how to do what i posted above without geting the error "global name is not defined".
Any help would be greatly appreciated
get_input = input
def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
main()
Looks like you need to just put all your raw_input statements inside your get_input function.
def get_input(currency):
currency = -1.0
while currency < 0:
try:
currency = float(raw_input("Enter %s: ", % (currency)))
except ValueError:
print "Invalid input!"
currency = -1.0
continue
if currency < 0:
print "Can't have negative money!"
else:
return currency
def main():
pennies = get_input("pennies")
nickles= get_input("nickles")
dimes= get_input("dimes")
quarters= get_input("quarters")
Then so on with your program

Struggling with a function that should return a total dollar amount

I need to create a function that returns a total of dollar amount. The problem is that I have no clue about how to exactly do this. I've tried to put in some code, but I don't believe it will return what I am looking for:
def main():
pennies = get_input("Enter pennies : ")
nickels = get_input("Enter nickels : ")
dimes = get_input("Enter dimes : ")
quarters = get_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : " , pennies)
print("\tNickels : " , nickels)
print("\tDimes : " , dimes)
print("\tQuarters : " , quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", total_value, sep="")
print("You have", dollars, "dollars and", left_over_cents, "cent(s)")
def get_input(currency):
currency = 0
if currency < 0:
print('Cannot have negative money')
else:
return currency
def get_total(pennies, nickels, dimes, quarters):
(pennies * .01) + (nickels * .05) + (dimes * .10) + (quarters * .25)
def get_dollars(pennies, nickels, dimes, quarters):
main()
Your prints are incorrect, and instead of get_input you should use raw_input
def main():
pennies = raw_input("Enter pennies : ")
nickels = raw_input("Enter nickels : ")
dimes = raw_input("Enter dimes : ")
quarters = raw_input("Enter quarters : ")
print("You entered : ")
print("\tPennies : %s" % pennies)
print("\tNickels : %s" % nickels)
print("\tDimes : %s" % dimes)
print("\tQuarters : %s" % quarters)
total_value = get_total(pennies, nickels, dimes, quarters)
dollars = get_dollars(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = %s$" % total_value)
print("You have"+ str(dollars) + "dollars and"+ str(left_over_cents)+ "cent(s)")
def get_input(currency):
currency = 0
if currency < 0:
print('Cannot have negative money')
else:
return currency
def get_total(pennies, nickels, dimes, quarters):
return (float(pennies) * .01) + (float(nickels) * .05) + (float(dimes) * .10) + (float(quarters) * .25)
def get_dollars(pennies, nickels, dimes, quarters):
return 1
main()
EDIT:
"I need to create a function that returns a total of dollar amount."
return was missing in your function get_total
The above code should be working

Categories