1

So for my school, I have to make this script that calculates the tip and tax of a meal. I'm ahead of everyone in my class, so no others have had this issue.

The code works fine in the Python3 IDLE on my PC, it also works fine at repl.it. Oddly enough, at my school's IDLE, which is private, and Python Fiddle, which is pretty much the same the tip and the tax do not properly calculate.

Also, there are some other bugs, such as displaying extra digits or not enough digits. I tried my hardest to fix this with string slicing but it didn't work. The only other way I know how to do it is with if statements which aren't allowed.

Any help would be appreciated, thanks in advance.

Code:

#Name
#9/20/16
#This program calculates the total cost of a meal.

def main():
    #INPUT
    meal = float(30.96)
    am = int(input("How many meals would you like? "))
    tx = int(input("What is the tax %? "))
    tp = int(input("How much % do you want to tip?" ))
#CALCULATIONS   
    subT = am*meal
    tax1 = tx/100
    tax = tax1*subT
    subTotalWithTax = subT + tax
    tip1 = tp/100
    tip = tip1*subTotalWithTax
    total = subTotalWithTax + tip
    clTip = str(tip)[0: 4]
    clTax = str(tax)[0: 4]
    clTotal = str(total)[0: 6]
    clSubT = str(subT)[0: 6]
#OUTPUT
    print("-----------------------------------------------")
    print("Items: ")
    print(str(am) + " Overloaded Potato Skins ------------- $7.99")
    print(str(am) + " Grilled Top Sirloin 12oz ------------ $16.49")
    print(str(am) + " Sweet Tea --------------------------- $1.99")
    print(str(am) + " Southern Pecan Pie ------------------ $3.99")
    print("------------------------------------------------")
    print("Totals: ")
    print("Subtotal: ----------------------------- $" + str(clSubT))
    print("Tax: ---------------------------------- $" + str(clTax))
    print("Tip: ---------------------------------- $" + str(clTip))
    print("Total --------------------------------- $" + str(clTotal))
    print("------------------------------------------------")
main()
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Eli Richardson
  • 934
  • 7
  • 25
  • 3
    Are you sure that at school you use Python 3? – vishes_shell Sep 20 '16 at 21:31
  • Check [this answer](http://stackoverflow.com/questions/1535596/what-is-the-reason-for-having-in-python). It could be a difference between python 2 and 3. – sir_lantzelot Sep 20 '16 at 21:32
  • Python Fiddle uses Python 2, and I bet the IDLE in your school is also for Python 2. The `/` operator behaves differently in Python 3 vs. Python 2. – edwinksl Sep 20 '16 at 21:33
  • @edwinksl That would be weird since the course I'm taking is teaching Python3. – Eli Richardson Sep 20 '16 at 21:36
  • @sir_lantzelot It is still not working with the double slashes. – Eli Richardson Sep 20 '16 at 21:47
  • @vishes_shell I think so. It does the same thing with double slashes and we use **print("")** instead of **print ""** – Eli Richardson Sep 20 '16 at 21:48
  • What is it that "doesn't work"? Show us output vs expected output. – Hugues Fontenelle Sep 20 '16 at 21:50
  • 3
    Put `from __future__ import division` at the very top of your file and see if that works, if it does then it is 100 percent a version issue. Using double slashes is definitely not going to help as that would floor in python3 and do nothing different in python2 . – Padraic Cunningham Sep 20 '16 at 21:56
  • @HuguesFontenelle The tip and the tax display as being $0 despite whatever is entered. – Eli Richardson Sep 20 '16 at 21:57
  • @PadraicCunningham **NotImplementedError: __future__ is not yet implemented in Skulpt on line 1** So it's a version issue? How do I fix? – Eli Richardson Sep 20 '16 at 22:00
  • @EliR, skuplt is a JS version of python so you are not even running from idle. – Padraic Cunningham Sep 20 '16 at 22:01
  • @PadraicCunningham American tax dollars being put to good use. If I can't get it fixed I'll email my teacher. – Eli Richardson Sep 20 '16 at 22:04
  • 1
    You just need to use floats for calculations, only some __future__ packages are implemented in skulpt, division is not one of them. If you run the code from http://www.skulpt.org/ you will see the exact same issue. You can also see a kind of python version `Python 2.6(ish) (skulpt, Tue Sep 20 2016 23:03:11 GMT+0100 (IST))`. i.e `python2.6(ish)` so you are definitely running a python2 version, also if you happen to ever have to input some non integer input you are going to get a NameError. input is basically `eval(raw_input())` in python2. – Padraic Cunningham Sep 20 '16 at 22:06

2 Answers2

1

The fact that you get $0 as answer may show that python (2, likely) still represents numbers as integers. Try explicitly casting to floats. Such as:

tip1 = tp/100.0  # instead of 100

or

tx = float(input("What is the tax %? "))

Also the clipping for displaying is a bit messy, try

print("Total --------------------------------- ${}".format(total))

The .format() is the trick you're looking for. There are ways to show only two decimals, check around SO or https://pyformat.info/ --but do try "{:.2f}".format(total) :-)


Edit Alternatively, without format: print("%.2f" % total)

And now, for a completely convoluted way to print the price (that is, if formatting is not allowed but string manipulation is):

totalDollar, totalCent = str(total).split('.')
totalCent += "00"
print("Total --------------------------------- $" + totalDollar + "." + totalCent[:2])
Hugues Fontenelle
  • 5,275
  • 2
  • 29
  • 44
0

I agree with edwinksl comment, check which version of python is on your schools computer. You can right click your python file and click edit with idle, the version should be in the top right coner of the page (next to the file path).

I have one other note however. Your teacher could have otherwise specified, but typically the subtotal is the total for the meal PLUS the tax. Then your tip gets calculated based on that and then added in. (unless your teacher said otherwise, follow their guidelines.)

subT = am*meal
tax1 = tx/100
tax = tax1*subT
subTotalWithTax = subT + tax
tip1 = tp/100
tip = tip1*subTotalWithTax
total = subTotalWithTax + tip
BLang
  • 930
  • 3
  • 16
  • 35
  • Thanks, I was really tired when I made the calculations part. – Eli Richardson Sep 20 '16 at 21:58
  • Not a problem, oh and if you want to specify the number of digits with floating point numbers, use this in a print statement. Note that it will round your answer based on what was in the thousandths placeholder! print("%.2f" % NUMBER) – BLang Sep 20 '16 at 22:05