3.13.1 zybooks challenge activity - python

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')

Related

Python change calculator

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

Lab #3 combining python codes after importing files where do I start?

Need help figuring out where to start after I import my two previous question files. Everything I've tried doesn't seem to want to take. So I scrapped it and trying to figure out where to begin again
1. In a file called FirstName_LastName_Main.py import Question_1.py and Question_2.py modules.
2. Prompt and read in the month followed by the day from the user. Note: month is of type String and day is of type int.
3. Call Seasons function from Question_1 module with the user’s input as arguments and print the season returned by the call to the function.
4. Prompt and read the total change amount from the user. Note: the total change is an integer.
5. Call the Exact_Change function from Question_2 module with the user’s input as it’s argument.
Sample Output:
Ex. If the input is:
Enter the month: April
Enter the day: 30
Output:
April 30: Spring
Ex. If the input is:
Enter the exact change in cents: 123
Output:
1 Dollar
2 Dimes
3 Pennies
question_1 file code I have -
month = input()
day = int(input())
if month in ('December', 'January', 'February'):
season = 'winter'
elif month in ('April', 'March ', 'May'):
season = 'spring'
elif month in ('June', 'July', 'August'):
season = 'summer'
else:
season = 'fall'
if (month == 'March') and (day >= 20) or (month == 'April') or (month == 'June') and (day >= 20):
season = 'spring'
elif (month == 'June') and (day <= 21) or (month == 'July') or (month == 'August') or (month == 'September') and (day >= 21):
season = 'summer'
elif (month == 'September') and (day >= 22) or (month == 'October') or (month == 'November') or (month == 'December') and (day >= 20):
season = 'fall'
elif (month == 'December') and (day <= 21) or (month == 'January') or (month == 'February') or (month == 'March') and (day >= 19):
season = 'winter'
print(season)
Question_2 file code I have -
amount = input()
if amount <= 0:
print(" No Change ")
else:
dollar = int(amount / 100)
amount = amount % 100
quarter = int(amount / 25)
amount = amount % 25
dime = int(amount / 10)
amount = amount % 10
nickel = int(amount / 5)
penny = amount % 5
if dollar >= 1:
if dollar == 1:
print(str(dollar)+" Dollar")
else:
print(str(dollar)+" Dollars")
if quarter >= 1:
if quarter == 1:
print(str(quarter)+" Quarter")
else:
print(str(quarter)+" Quarters")
if dime >= 1:
if dime == 1:
print(str(dime)+" Dime")
else:
print(str(dime)+" Dimes")
if penny >= 1:
if penny == 1:
print(str(penny)+" Penny")
else:
print(str(penny)+" Pennies")
if nickel >= 1:
if nickel == 1:
print(str(nickel)+" Nickel")
else:
print(str(nickel)+" Nickels")
#SSince it decided to show weirdly above sharing the files below. This is question_2
...
amount = input()
if amount <= 0:
print(" No Change ")
else:
dollar = int(amount / 100)
amount = amount % 100
quarter = int(amount / 25)
amount = amount % 25
dime = int(amount / 10)
amount = amount % 10
nickel = int(amount / 5)
penny = amount % 5
if dollar >= 1:
if dollar == 1:
print(str(dollar)+" Dollar")
else:
print(str(dollar)+" Dollars")
if quarter >= 1:
if quarter == 1:
print(str(quarter)+" Quarter")
else:
print(str(quarter)+" Quarters")
if dime >= 1:
if dime == 1:
print(str(dime)+" Dime")
else:
print(str(dime)+" Dimes")
if penny >= 1:
if penny == 1:
print(str(penny)+" Penny")
else:
print(str(penny)+" Pennies")
if nickel >= 1:
if nickel == 1:
print(str(nickel)+" Nickel")
else:
print(str(nickel)+" Nickels")

I have to make this calendar program and it isn't running

Here is my code that isn't running. It's a calendar program that counts days, days left in year, and leap years.
def leap_year():
if y % 400 == 0:
return 1
elif y % 100 == 0:
return 0
elif y % 4 == 0:
return 1
else:
return 0
def number_of_days(m, y):
y = leap_year(y)
if (m == 1 or m == 3 or m == 5 or m == 7 or m == 8 or m == 10 or m == 12):
return 31
elif (m == 4 or m == 6 or m == 9 or m == 11):
return 30
elif (m == 2):
if (y == 1):
return 29
elif (y == 0):
return 28
print(m)
def days_left(d, m, y):
daysleft = 0
for i in range(m,13):
days_left += number_of_days(m, y)
m += 1
print(days_left - d)
print("Please enter a date")
d = int(input("Day: "))
m = int(input("Month: "))
y = int(input("Year: "))
print("Menu: ")
print("1) Calculate the number of days in the given month: ")
print("2) Calculate the number of days left in the given year: ")
menu = int(input())
if menu == 1:
print(number_of_days(m, y))
elif menu == 2:
print(days_left(d, m, y))
else:
print("Invalid choice")
The first three errors in your code are:
function leap_year is declared with 0 parameters and is called with 1.
In the function days_left you have an assignment daysleft = 0 and then `days_left += 1
Wrong indentation in the same function # m += 1

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.

Categories

Resources