user3285386
user3285386

Reputation: 45

I am getting an unexpected character after line continuation character error

I am getting an unexpected character after line continuation character error in this line

 print (\t,"Order total $",format(total, "10.2"),\n\t,"Discount    $",format(disc,"10.2"),\n\t,"Amount Due $",format (due, "10.2"),sep="")

could someone tell me what that means and how to fix it? thanks

def finddiscount(quantity):
        if quantity >= 1 and quantity <= 9:
            discount = 0
        elif quantity >= 10 and quantity <= 19:
            discount = .2
        elif quantity >= 20 and quantity <= 49:
            discount = .30
        elif quantity >= 50 and quantity <= 99:
            discount = .40
        elif quantity >= 100:
            discount = .50
    def calctotal(quantity, price):
        disc = (price*quantity)*finddiscount(quantity)
        total = (price*quantity)
        due = (price*quantity)-(price*quantity)*dicount
        print (\t,"Order total $",format(total, "10.2"),\n\t,"Discount    $",format(disc,"10.2"),\n\t,"Amount Due $",format (due, "10.2"),sep="")
    def main():
        quantity = int(input("How many packages where purchased?"))
        price = float(input("How much is each item?"))
        calctotal(quantity, price)
    main()

Upvotes: 0

Views: 21502

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251051

You've forgot to use quotes around many items on this line:

print ("\t","Order total $",format(total, "10.2"),"\n\t","Discount    $",format(disc,"10.2"),"\n\t","Amount Due $",format (due, "10.2"),sep="")
        ^                                           ^                                          ^

And another way to format is to use str.format like this:

print ("\tOrder total $ {:10.2}\n\tDiscount    ${:10.2}\n\tAmount Due ${:10.2}".format(total, disc, due))

Upvotes: 5

abarnert
abarnert

Reputation: 365915

Ashwini's answer explains why your code gives the error it does.

But there's a much simpler way to do this. Instead of printing a bunch of strings separated by commas like this, just put the strings together:

print("\tOrder total $", format(total, "10.2"),
      "\n\tDiscount    $", format(disc, "10.2"),
      "\n\tAmount Due $", format(due, "10.2"), sep="")

(I also fixed everything to fit on an 80-column screen, which is a standard for good reasons—for one thing, it's actually readable on things like StackOverflow; for another, it makes it much more obvious that your code doesn't actually line up the way you wanted it to…)

In this case, it would probably be even better to use three separate print calls. Then you don't need those \n characters in the first place:

print("\tOrder total $", format(total, "10.2"), sep="")
print("\tDiscount    $", format(disc, "10.2"), sep="")
print("\tAmount Due $", format(due, "10.2"), sep="")

Meanwhile, since you're already using the format function, you should have no trouble learning about the format method, which makes things even simpler. Again, you can use three separate statements—but in this case, maybe a multi-line (triple-quoted) string would be easier to read:

print("""\tOrder total ${:10.2}
\tDiscount    ${:10.2}
\tAmount Due ${:10.2}""".format(total, disc, due))

See the tutorial section on Fancier Output Formatting for more details on all of this.

Upvotes: 1

Related Questions