I'm a beginner to Python and am struggling with a project I am working on. The program takes monthly income, fixed monthly expenses, asks for any additional expenses, and then outputs with an if else statement. My issue is in this function:
def spending(income, totals):
finalTotal = income - totals
if income > finalTotal:
print("You saved $", "%.2f"%finalTotal, "this month!")
elif finalTotal > income:
print("You overspent by $", "%.2f"%finalTotal, "this month!")
else:
print("You broke even this month!")
It is printing:
You saved $ -805.00 this month!
When I'd like it to print:
You overspent by $ 805.00 this month!
Any feedback would be greatly appreciated!!
Here is my code:
https://replit.com/#ashleyshubin/BudgetProgram#main.py
If totals is your monthly expenses, then subtracting totals from income gives you how much money you made this month. The first if statement should be
if finalTotal > 0:
The second if statement would be
if finalTotal < 0:
And you would want to use abs(finalTotal) when printing finalTotal so it does not display a negative number
Your checks are wrong. You can do the following:
def spending(income, totals):
finalTotal = income - totals
if income > totals:
print("You saved $", "%.2f"%finalTotal, "this month!")
elif totals > income:
print("You overspent by $", "%.2f"%-finalTotal, "this month!")
else:
print("You broke even this month!")
This section is where it is going wrong:
finalTotal = income - totals
if income > finalTotal:
print("You saved $", "%.2f"%finalTotal, "this month!")
It looks like you're comparing the wrong variables in your "if" statement. your "finalTotal" variable already includes your income. Think of it like alegbra, and replace "finalTotal" with "income - totals", and your if statement now looks like:
if income > (income - totals):
this statement will always evaluate to true, no matter what values you are using. You probably want to rewrite your if statements to compare "income > totals", rather than finalTotal.
your comparison shoud be based on finaltotal , as fianaltotal is finalTotal = income - totals, also else never runs based on the other conditions in your code:
def spending(income, totals):
finalTotal = income - totals
if finalTotal > 0:
print("You saved $", "%.2f" % finalTotal, "this month!")
else:
print("You overspent by $", "%.2f" % abs(finalTotal), "this month!")
you should be comparing Income with total spends.
def spending(income, totals):
finalTotal = abs(income - totals)
if totals < income:
print("You saved $", "%.2f"%finalTotal, "this month!")
elif totals > income:
print("You overspent by $", "%.2f"%finalTotal, "this month!")
else:
print("You broke even this month!")
Related
Hi I'm working on an assignment where I make a self-service program. I'm having trouble trying to incorporate a while loop while collecting user input. I want my code to collect an amount of money (entered by the user) and prompt the user on how much more they need to pay. Once the amount paid exceeds the amount due, I want to continue with the rest of my code. This is what I have so far but the loop stops after 2 inputs, even if the user has not paid enough.
elif paid < TotalCost:
due = TotalCost - paid
while due > 0:
print("You need to pay $",round(due,2)," more.")
morepaid=float(input("Enter the amount paid: $"))
if morepaid>TotalCost:
change= morepaid- TotalCost
print("Change due: $",round(change,2))
print(" ")
print("Thank you for visiting McDonald's")
elif due== 0:
print("Change due: $0.00")
print(" ")
print("Thank you for visiting McDonald's")
else:
due -= morepaid
You can simply subtract the amount paid from due, you don't need another variable totalpaid.
elif paid < totalCost:
due = totalCost - paid:
while due > 0:
print("You need to pay $",round(due,2)," more.")
morepaid=float(input("Enter the amount paid: $"))
due -= morepaid
I have a program I need to write for college and I have an error in my code that I don't know how to fix
#calculate savings and print users total cost
if voucher_quant < 20:
print("Your total will be £", str(voucher_value*voucher_quant))
elif voucher_quant => 20 and voucher_quant=<40:
print("Your total will be £", str((voucher_value*voucher_quant)*0.05)
elif voucher_quant =>41 and voucher_quant =<70:
print("Your total will be £", str((voucher_value*voucher_quant)*0.075)
elif voucher_quant =>71 and voucher_quant =<100:
print("Your total will be £", str((voucher_value*voucher_quant)*0.1)
can anyone help me on how to fix this?
elif has to be at same identation level as its if is.
Comparison operators are like <= and not =<. = would be latter
Your print statements do not complete brackets
This code is correct syntactically
voucher_quant = 50 # Change as you want
voucher_value = 10 # Change as you want
if voucher_quant < 20:
print("Your total will be £", str(voucher_value*voucher_quant))
elif voucher_quant >= 20 and voucher_quant<=40:
print("Your total will be £", str((voucher_value*voucher_quant)*0.05))
elif voucher_quant >= 41 and voucher_quant <= 70:
print("Your total will be £", str((voucher_value*voucher_quant)*0.075))
elif voucher_quant >= 71 and voucher_quant<=100:
print("Your total will be £", str((voucher_value*voucher_quant)*0.1))
#Output
#Your total will be £ 37.5
My teacher wants this code to run on a loop but I am not sure how to do that. Can anyone help me please?
ask = input("Enter only one name of the item bought like Potatoes, Ham, AppleJuice, or Gatorade: ")
if ask == "potatoes":
def potatoes (quantity, price=5.99):
total = quantity * price
while True:
if quantity > 15:
print("Congratulations, you are eligible for 11% discount")
totalWithDiscount = total - (0.08 * total)
print("your total with 8% discount and no tax: $", totalWithDiscount)
print("Visit us soon!!")
return (0.879 * totalWithDiscount) + totalWithDiscount
else:
print("sorry, you are not eligible for 11% discount")
print("your total with 11% discount and no tax: $", total)
return (0.879 * total) + total
print("Sorry, you are only eligible for discount, if you buy more than 15 Lbs. of Potatoes. ")
print("Total without discount and no Tax $", total)
print("Your Total with Tax: 5.29% is $", tax3)
print("Thank you, for being Walmart' Valuable Shopper.")
print("Visit us soon!!")
potatoes(int(input("Enter the quantity of Potatoes you bought : ")), 8.99)
Your code is very wrong and it will not run, but I have an idea of what you are trying to do, so this will work
def potatoes (quantity, price=5.99):
total = quantity * price
if quantity > 15:
print("Congratulations, you are eligible for 11% discount")
totalWithDiscount = total - (0.08 * total)
print("your total with 8% discount and no tax: $", totalWithDiscount)
print("Visit us soon!!")
return (0.879 * totalWithDiscount) + totalWithDiscount
else:
print("sorry, you are not eligible for 11% discount")
print("your total with 11% discount and no tax: $", total)
tax3 = (0.879 * total) + total
print("Sorry, you are only eligible for discount, if you buy more than 15 Lbs. of Potatoes. ")
print("Total without discount and no Tax $", total)
print("Your Total with Tax: 5.29% is $", tax3)
print("Thank you, for being Walmart' Valuable Shopper.")
print("Visit us soon!!")
while True:
ask = input("Enter only one name of the item bought like Potatoes, Ham, AppleJuice, or Gatorade: ")
if ask == "potatoes":
number_of_potatoes = int(input("Enter the quantity of Potatoes you bought : "))
potatoes(number_of_potatoes, 8.99)
else:
print('Thank you visiting')
break
Your code is a little bit confusing but i hope this is what you intended for.
1.I moved the function definition out of the loop, and it is inside the loop that you call it so the code inside the function can be reused as many times as you need!
2.I also changed the loop condition for when the user inserts "no" so there's an end to it, but change for whatever you want
3.About the return statement it's important to remember that when it's executed the function that it is within ends and return the designated value, everything after it won't be executed, that's why i moved it to the end.
4.And finally there's a variable tax3 that isn't being defined anywhere in your code, maybe you didn't put it here or maybe you just forgot about it, so you need to fix it or delete it so the code works. Hope i understood your question, good programming!
def potatoes (quantity, price=5.99):
total = quantity * price
if quantity > 15:
print("Congratulations, you are eligible for 11% discount")
totalWithDiscount = total - (0.08 * total)
print("your total with 8% discount and no tax: $", totalWithDiscount)
print("Visit us soon!!")
return (0.879 * totalWithDiscount) + totalWithDiscount
else:
print("sorry, you are not eligible for 11% discount")
print("your total with 11% discount and no tax: $", total)
print("Sorry, you are only eligible for discount, if you buy more than 15 Lbs. of Potatoes. ")
print("Total without discount and no Tax $", total)
print("Your Total with Tax: 5.29% is $", tax3)
print("Thank you, for being Walmart' Valuable Shopper.")
print("Visit us soon!!")
return (0.879 * total) + total
ask = input("Enter only one name of the item bought like Potatoes, Ham, AppleJuice, or Gatorade: ")
while(ask != "no"):
if ask == "potatoes":
potatoes(int(input("Enter the quantity of Potatoes you bought : ")), 8.99)
ask = input("Enter only one name of the item bought like Potatoes, Ham, AppleJuice, or Gatorade: ")
I wrote a script and it works on Anaconda. However, on the website (hosted by namecheap), the input function doesn't seem to work, what is wrong?
Website address: http://xterraminer.com.au/cgi-bin/nctests.py
Have already set up namecheap python to Django, not sure if this helped.
> user_input=float(input("What is your income?")) tax=float()
> take_home=float() if user_input<=18200:
> print("You pay $0 tax") elif user_input>18200 and user_input<=37000:
> tax=(user_input-18200)*0.19
> print("Your tax is $",tax) elif user_input>37000 and user_input<=90000:
> tax=(user_input-37000)*0.325+3572
> print("Your tax is $",tax) elif user_input>90000 and user_input<=180000:
> tax=(user_input-18200)*0.37+20797
> print("Your tax is $",tax) elif user_input>180000:
> tax=(user_input-18200)*0.45+54097
> print("Your tax is $",tax) take_home=user_input-tax print("Your take home pay is $", take_home)
Would expect the website asking for an input
user = str
end = False
hours = round(40,2)
print("How much do you make?")
while end == False:
user = input("\nPlease enter your name or type '0' to quit: ")
if user == "0":
print("End of Report")
break
else:
hours = (float(input("Please enter hours worked: ", )))
payrate =(float(input("Please enter your payrate: $", )))
taxrate = (float(input ("Please enter tax rate: ")))
if hours <= 40:
print(user)
print("Overtime hours: 0")
print("Overtime Pay: $0.00")
regularpay = round(hours * payrate, 2)
print("Gross Pay: $", regularpay)
elif hours > 40:
overtimehours = round(hours - 40.00,2)
print("Overtime hours: ", overtimehours)
print("Employee's name: ", user)
regularpay = round(hours * payrate,2)
overtimerate = round(payrate * 1.5, 2)
overtimepay = round(overtimehours * overtimerate)
if overtimepay == 0:
grosspay = round(regularpay,2)
else overtimepay > 0:
grosspay = round(regularpay+overtimepay,2)
income = (grosspay * taxrate)
print("Regular Pay: $", regularpay)
print("Overtime Pay: $",overtimepay)
print("Gross Pay: $", grosspay)
print ("Income: $", income)
I added that extra If/Else statement to hopefully force it it through but that still didnt seem to get it to work. Even if you remove the second else if statement it still does not get it to print, only when you do have over time then it factors in the tax rate.
You only set overtimepay when you actually process overtime. Undefined variables in python are not 0. They are a special value None. Therefore if no overtime was worked, neither of your ifs evaluates to True and neither branch gets executed.