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:
Related
I need to create a program that will input a money amount in the form of a floating point number. The program will then calculate which dollars and coins to make this amount. Coins will be preferred in the least number of coins. If any of the values is zero, I need to not output the value. IE: if the change is 26 cents you only need to tell the user they will receive 1 quarter and 1 penny. No more, no less.
Below is what I have so far, the only thing I cant figure out is to make the program not output the zero values
# calculate amount of change needed
dollar = 100
quarter = 25
dime = 10
nickel = 5
penny = 1
def main():
calc = True
while calc:
lst = []
mon = ['dollars', 'quaters', 'dimes', 'nickels', 'pennys']
doll = 0
quart = 0
dimes = 0
nick = 0
pen = 0
total = int(float(input('Enter amount of change: '))*100)
amount = total
while amount - dollar >= 0:
amount -= dollar
doll += 1
while amount - quarter >= 0:
amount -= quarter
quart += 1
while amount - dime >= 0:
amount -= dime
dimes += 1
while amount - nickel >= 0:
amount -= nickel
nick += 1
while amount - penny >= 0:
amount -= penny
pen += 1
lst.append(doll)
lst.append(quart)
lst.append(dimes)
lst.append(nick)
lst.append(pen)
print('\nThe change owed is: ')
print(" ")
for i, e in zip(lst, mon):
print(i, e)
calc = input("\nPress 'Y' to try again or any other key to exit: ")
if calc != 'y' and calc != 'Y':
calc = False
if __name__ == "__main__":
main()
else:
pass
I ended up solving it
if doll >= 1:
lst.append(doll)
mon.append("dollars")
if quart >= 1:
lst.append(quart)
mon.append("quarters")
if dimes >= 1:
lst.append(dimes)
mon.append("dimes")
if nick >= 1:
lst.append("nickles")
if pen >= 1:
lst.append(pen)
mon.append("pennies")
this seems to have worked
So im having trouble with this Activity i've got it mostl figured out but whenever it is given an input of 45. the output is only 2 Dimes. and nothing else. but 46 and 44 and everything else gives me the perfect output. whats wrong?
pennies=int(input())
dollars = 0
quarters = 0
dimes = 0
nickels = 0
if pennies == 0:
print('No change')
if pennies > 100:
dollars = int(pennies/100)
pennies = (pennies%100)
if pennies > 25:
quarters = (pennies//25)
pennies = (pennies%25)
if pennies > 10:
dimes = int(pennies/10)
pennies = pennies%10
if pennies > 5:
nickels = int(pennies/5)
pennies = (pennies%5)
if (dollars > 1):
print ( dollars,'Dollars')
elif (pennies > 0) and (dollars == 1):
print (dollars, 'Dollar')
if (quarters > 1):
print ( quarters,'Quarters')
elif (pennies > 0) and (quarters == 1):
print (quarters, 'Quarter')
if (dimes > 1):
print ( dimes,'Dimes')
elif (pennies > 0) and (dimes == 1):
print (dimes, 'Dime')
if (nickels > 1):
print ( nickels,'Nickels')
elif (pennies > 0) and (nickels == 1):
print (nickels, 'Nickel')
if (pennies > 1):
print ( pennies,'Pennies')
elif (pennies > 0) and (pennies == 1):
print (pennies, 'Penny')
I am solving the following problem, and came across the situation where I'm unable to define either decision operators or the while loop properly.
The task:
Customers get a monthly discount depending on the length of contract they take out as shown below:
3 – 6 months 2% discount
7 – 12 months 5% discount
over 12 months 10% discount
The program should ask users for their name and the monthly cost of their game package. It should then ask them to enter the contract length they would like. The maximum contract length is 18 months. The program should finally display the entered details and the final cost of the package with the discount applied.
contract_length = 0
final_cost = 0
#prompt the user to enter their name
user_name = input("What is your name? ")
#prompt the user to enter monthly cost for the game package
package_cost = float(input("Monthly cost of your game package: "))
while contract_length > 0 and contract_length <= 18:
#prompt the user to enter the contract length
contract_length = int(input("Enter the contract length you would like: "))
#selection statement to calculate final cost
if contract_length > 12:
discount = (package_cost * 0.1)
final_cost = format(package_cost - discount,".2f")
elif contract_length > 6:
discount = (package_cost * 0.05)
final_cost = format(package_cost - discount, ".2f")
elif contract_length >= 3:
discount = (package_cost * 0.02)
final_cost = format(package_cost - discount, ".2f")
else:
print("Invalid entry")
#display results
print("Name " +user_name)
print("Package Cost £",str(package_cost))
print("Months in Contract " , str(contract_length))
print("The final cost is £",str(final_cost))
Error I am having
What is your name? Jhon
Monthly cost of your game package: 35.12
Name Jhon
Package Cost £ 35.12
Months in Contract 0
The final cost is £ 0
>>>
I even tried "and" to "or" but with or operator it's start repeating the input function under while loop. Any help? Thanks.
I would recommend you to have a condition on the final_cost, like this:
contract_length = 0
final_cost = 'none'
#...
while final_cost == 'none':
#prompt the user to enter the contract length
contract_length = int(input("Enter the contract length you would like: "))
if contract_length > 18:
print("Invalid entry")
continue
#selection statement to calculate final cost
if contract_length > 12:
discount = (package_cost * 0.1)
final_cost = format(package_cost - discount,".2f")
elif contract_length > 6:
discount = (package_cost * 0.05)
final_cost = format(package_cost - discount, ".2f")
elif contract_length >= 3:
discount = (package_cost * 0.02)
final_cost = format(package_cost - discount, ".2f")
else:
print("Invalid entry")
Here is another solution:
contract_length = 0
final_cost = 0
package_cost = 0
user_name = None
while not user_name:
user_name = input("What is your name? ")
while package_cost <= 0:
package_cost = input("Monthly cost of your game package: ")
if not package_cost.isdigit():
package_cost = 0
package_cost = float(package_cost)
while not (3 <= contract_length <= 18):
contract_length = input("Enter the contract length you would like: ")
if not contract_length.isdigit():
contract_length = 0
contract_length = int(contract_length)
if contract_length > 12:
discount = 0.1
elif contract_length > 6:
discount = 0.05
elif contract_length >= 3:
discount = 0.02
else:
discount = 0
final_cost = package_cost - (package_cost * discount)
print("Name", user_name)
print("Package Cost £", format(package_cost, ".2f"))
print("Months in Contract", contract_length)
print("The final cost is £", format(final_cost, ".2f"))
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.
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.