Why wont my program add up my change in python? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
When I run my program it runs and everything but when I wanna see the total to my input I get
"total = $ none" and I don't know why it keeps saying that?
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, cents = get_left_over_cents(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", cents, "cent(s)")
def get_input1(pennies):
pennies = 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 = 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 = 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 = input("Enter quarters: ")
if int(quarters) < 0:
print("Error: money cannot be negative")
pennies = int(input("Enter correct amount of quarters: "))
return quarters
def get_total(pennies, nickels, dimes, quarters):
amount_pennies = (int(pennies) * .01)
amount_nickels = (int(nickels) * .05)
amount_dimes = (int(dimes) * .10)
amount_quarters = (int(quarters) * .25)
def get_left_over_cents(pennies, nickels, dimes, quarters):
total = int(pennies) + 5*int(nickels) + 10*int(dimes) + 25*int(quarters)
return total // 100, total % 100
main()

You are not returning anything from get_total function.
def get_total(pennies, nickels, dimes, quarters):
amount_pennies = (int(pennies) * .01)
amount_nickels = (int(nickels) * .05)
amount_dimes = (int(dimes) * .10)
amount_quarters = (int(quarters) * .25)
return amount_pennies + amount_nickels + amount_dimes + amount_quarters
In Python, if a function doesn't explictly return any value, it returns None by default.

Related

TypeError: mock_input_output_start.<locals>.<lambda>() takes 1 positional argument

Here is my line of code:
if change < 100:
quarters, dimes, nickels, pennies = coin_calc(change)
print(quarters, "quarter(s)")
print(dimes, "dime(s)")
print(nickels, "nickle(s)")
print(pennies, "penny(ies)")
print("Want to calculate another amount? (y/n): ", end="")
answer = input()
if answer == "n":
print("Bye!")
break
I get the error: TypeError: mock_input_output_start..() takes 1 positional argument but 2 were given
What can I do to fix this?
I tried:
while True:
change = input("Enter change amount to convert: ")
if str(change).isnumeric():
change = int(change)
if change < 100:
quarters, dimes, nickels, pennies = coin_calc(change)
print(quarters, "quarter(s)")
print(dimes, "dime(s)")
print(nickels, "nickle(s)")
print(pennies, "penny(ies)")
print("Want to calculate another amount? (y/n): ", end="")
answer = input()
if answer == "n":
print("Bye!")
break
else:
print("Error! Invalid integer entered please try again.")
else:
print("Error! Invalid integer entered please try again.")
def coin_calc(change):
quarters = change // 25
change = change % 25
dimes = change // 10
change = change % 10
nickels = change // 5
change = change % 5
pennies = change
return quarters, dimes, nickels, pennies
Expecting a program to exchange coins.
I receive the TypeError

Having trouble getting this loop to run properly

I am a first year programming student and I am having trouble trying to get my code to loop properly and I would like some pointers on how I can get it to run as desired. I am running a program that will calculate change with a preset stock of 10 of each coin, price and payment will be input by the user and change will be returned by the number of coins left in the stock. Right now i am having difficulties trying to get the program to return the 'insufficient funds' and the 'no change' print statements and trying to get it to loop over from quarters to dimes to nickels and pennies properly, I am not sure what I could be doing wrong.
quarters = 10
dimes = 10
nickels = 10
pennies = 10
stock = quarters, dimes, nickels, pennies
print("\nWelcome to change-making program.")
print("\nStock: {} quarters, {} dimes, {} nickels, and {} pennies".format(
quarters, dimes, nickels, pennies))
in_str = float(input("Enter the purchase price (xx.xx) or 'q' to quit: "))
payment_int = int(input("Input dollars paid (int):"))
change_sum = float(payment_int) - in_str
#in_str = float(input("Enter the purchase price (xx.xx) or 'q' to quit: "))
while change_sum >= 0:
if payment_int < in_str:
print("Insufficient funds")
break
elif payment_int == in_str:
print("No Change")
break
else:
if quarters > 0 and change_sum >= 0.25:
change_sum = change_sum - 0.25
quarters -= 1
print(quarters, change_sum)
elif dimes > 0 and 0.25 > change_sum >= 0.10:
change_sum = change_sum - 0.1
dimes -= 1
print(dimes, change_sum)
elif nickels > 0 and 0.1 > change_sum >= 0.05:
change_sum = change_sum - 0.05
nickels -= 1
print(nickels, change_sum)
elif pennies > 0 and 0.05 > change_sum >= 0.01:
change_sum = change_sum -0.01
pennies -=1
print(pennies, change_sum)
else:
if change_sum == 0.0:
break
print("\nStock: {} quarters, {} dimes, {} nickels, and {} pennies".format(
quarters, dimes, nickels, pennies))
print(change_sum)
If the fund is insufficient, then change_sum is <0 and the while loop never runs. That's why you don't get "insufficient fund" message. You should first check the sufficiency and then start the loop:
Just change this part:
while change_sum >= 0:
if payment_int < in_str:
print("Insufficient funds")
break
elif payment_int == in_str:
print("No Change")
break
else:
if quarters > 0 and change_sum >= 0.25:
to this:
if payment_int < in_str:
print("Insufficient funds")
break
elif payment_int == in_str:
print("No Change")
break
while change_sum >= 0:
if quarters > 0 and change_sum >= 0.25:

Python change machine. Issues with calculating correct values

I dont really know what is wrong with my code. I've been working on it for a couple of days, and have posted mulitple times on this forum, but to no avail. This is the output i receive:
The results for stock should be 8 q, 10 d, 9 n, 8 p
Also the final stock in a previous iteration needs to be the starting stock for the next iteration and so on. That is why I have the while loop. At this point if you can correct my code and post it that would be fine, I don't really care (if you really want to, even though I dont expect anyone will). Hints are great! I honestly think that the issue is syntactical or in the while loop. Any help would be beneficial!
P.s. I have to use loops, functions aren't allowed
pennies = 10
nickels = 10
dimes = 10
quarters = 10
quarters_spent = 0
dimes_spent = 0
nickels_spent = 0
pennies_spent = 0
print("\nWelcome to change-making program.")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")
while in_str.lower() != 'q':
dollar_str, cents_str = in_str.split(".")
if in_str.lower() == 'q':
quit()
in_int = int(float(in_str) * 100)
if in_int < 0:
print("Error: purchase price must be non-negative.")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")
if in_int > 0:
payment = input("\nInput dollars paid: ")
payment_int = int(float(payment) * 100)
change = payment_int - in_int
#determines if there payment input
if payment_int < in_int:
print("Error: Insufficient payment.")
payment = input("\nInput dollars paid: ")
payment_int = int(float(payment) * 100)
change = payment_int - in_int
if change == 0:
print("No change.")
#determines how many quarters, dimes, nickels, and pennies are left
while change >= 25 and quarters > 0:
change = change - 25
quarters_spent += 1
quarters = quarters - quarters_spent
while change >= 10 and dimes > 0:
change = change - 10
dimes_spent += 1
dimes = dimes - dimes_spent
while change >= 5 and nickels > 0:
change = change - 5
nickels_spent += 1
nickels = nickels - nickels_spent
while change >= 1 and pennies > 0:
change = change - 1
pennies_spent += 1
pennies = pennies - pennies_spent
if quarters == 0 and dimes == 0 and nickels == 0 and pennies == 0:
print("Error: ran out of coins.")
quit()
print("\nCollect Payment Below:")
if quarters_spent > 0:
print(quarters_spent, "Quarters")
if dimes_spent > 0:
print(dimes_spent, "Dimes")
if nickels_spent > 0:
print(nickels_spent, "Nickels")
if pennies_spent > 0:
print(pennies_spent, "Pennies")
print("\nStock: ", quarters, "Quarters, ", dimes, " Dimes, ", nickels, " Nickels, ", pennies, " Pennies ")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")
pennies = pennies
nickels = nickels
dimes = dimes
quarters = quarters
The problem is lines like this one:
quarters = quarters - quarters_spent
Each time through the loop, you're adding 1 to quarters_spent. So the first time you subtract 1 from quarters, the next time you subtract 2 from quarters, and so on. You should just subtract 1 from quarters each time.
while change >= 25 and quarters > 0:
change -= 25
quarters_spent += 1
quarters -= 1
and similarly for the other coins.
Also, if each purchase is just supposed to show the change for that purchase, not the accumulated change from all purchases, you should set quarters_spent, dimes_spent, nickels_spent, and pennies_spent to 0 inside the loop, before calculating the change.

cannot separate dollars and cents python

This function now works but the problem I am now having is separating the dollars from cents. It keeps giving me the same dollars as left_over_cents. How can I just show the cents?
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 % 1
return left_over_cents
main()
You want to remove the dollars from the total value, for instance by something like
total_value = get_total(pennies, nickels, dimes, quarters)
left_over_cents = total_value % 1
(% 1 gives the remainder after dividing by 1)

Why does my code keep asking me for quarters 4 times?

I have this homework assignment and I'm not suppose to change this part of the code
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, cents = get_left_over_cents(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", format(total_value,".02f"), sep="")
print('You have', dollars, "dollars and", cents, "cent(s)")
This is my code for the assignment that I've done
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, cents = get_left_over_cents(pennies, nickels, dimes, quarters)
left_over_cents = get_left_over_cents(pennies, nickels, dimes, quarters)
print("Total = $", format(total_value,".02f"), sep="")
print('You have', dollars, "dollars and", cents, "cent(s)")
def get_input(pennies):
pennies = input("Enter Pennies: ")
while int(pennies) < 0:
print("Error: money cannot be negative")
pennies = int(input("Enter correct amount of pennies: "))
return pennies
def get_input(nickels):
nickels = input("Enter nickels: ")
while int(nickels) < 0:
print("Error: money cannot be negative")
nickels = int(input("Enter correct amount of nickels: "))
return nickels
def get_input(dimes):
dimes = input("Enter dimes: ")
while int(dimes) < 0:
print("Error: money cannot be negative")
dimes = int(input("Enter correct amount of dimes: "))
return dimes
def get_input(quarters):
quarters = input("Enter quarters: ")
while int(quarters) < 0:
print("Error: money cannot be negative")
quarters = int(input("Enter correct amount of quarters: "))
return quarters
def get_total(pennies, nickels, dimes, quarters):
amount_pennies = (int(pennies) * .01)
amount_nickels = (int(nickels) * .05)
amount_dimes = (int(dimes) * .10)
amount_quarters = (int(quarters) * .25)
return amount_pennies + amount_nickels + amount_dimes + amount_quarters
def get_left_over_cents(pennies, nickels, dimes, quarters):
total = int(pennies) + 5*int(nickels) + 10*int(dimes) + 25*int(quarters)
return total // 100, total % 100
main()
But everytime I run the code, it tells me to enter quarters 4 times. But I want it to say enter pennies, enter nickels, and so forth. Can someone help me? I'm really confused on how to make it do that without changing that core code
You keep redefining your get_input function. Each time you do so, the old get_input function is overshadowed and becomes no longer available. So, in the end, you only have the get_input you made for quarters.
You need to rename your functions so that each has a unique name that will not overshadow another function. Why not do something like:
def get_pennies(...):
...
def get_nickels(...):
...
def get_dimes(...):
...
def get_quarters(...):
...
Or, even better, make a single function that can handle all coin types. A (basic) example is below:
def get_coin(coin_type):
amount = int(input("Enter " + coin_type + ": ")) # BTW, you forgot to call int() up here before.
while amount < 0:
print("Error: money cannot be negative")
amount = int(input("Enter correct amount of " + coin_type + ": "))
return amount
You would then call this function as:
pennies = get_coin('pennies')
nickels = get_coin('nickels')
...
As iCodez explains, every time you write def get_input(dimes): or def get_input(quarters):, you're just creating a new definition for the function get_input, and hiding the old one. The fact that the parameter has a different name doesn't mean anything. (How could it? When you write pennies = get_input("Enter pennies : "), how would Python know that you want to call get_input(pennies) instead of get_input(quarters)?)
You can fix this by giving each one a unique name (get_pennies, get_nickels, etc.), and calling them by the unique names.
Also, notice that you're not actually using the argument pennies or nickels or whatever. You're taking a prompt string, and doing nothing with it.
And that's important, because it means you can refactor them all into one function. If you look at the implementations you've written, they're already almost identical; the only difference is in the prompt strings you print out. And you're already getting a different prompt string with each call. So, why not just use it?
def get_input(prompt):
number = input(prompt)
while int(number) < 0:
print("Error: money cannot be negative")
number = int(input(prompt))
return number
Now, pennies = get_input("Enter pennies : ") will ask you for pennies, nickels = get_input("Enter nickels : ") will ask you for nickels, etc.
As a side note, there's a serious bug in all four of your implementations (and in my merged version(). If your first input is non-negative, the function will return the string, like "3"; if that one is negative, but a later one is not, the function will return the integer value, like 5. You probably want to make the first number = … and the second one identical.

Categories

Resources