Python 换行符错误后我得到一个意外的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21843580/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I am getting an unexpected character after line continuation character error
提问by user3285386
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()
回答by Ashwini Chaudhary
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.formatlike this:
另一种格式化方法是这样使用str.format:
print ("\tOrder total $ {:10.2}\n\tDiscount    ${:10.2}\n\tAmount Due ${:10.2}".format(total, disc, due))
回答by abarnert
Ashwini's answer explains why your code gives the error it does.
Ashwini 的回答解释了为什么您的代码会出现错误。
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…)
(我还修复了所有内容以适合 80 列屏幕,这是一个有充分理由的标准——一方面,它实际上在 StackOverflow 之类的东西上是可读的;另一方面,它更明显地表明您的代码实际上并不按照您希望的方式排列……)
In this case, it would probably be even better to use three separate printcalls. Then you don't need those \ncharacters in the first place:
在这种情况下,使用三个单独的print调用可能会更好。那么你\n首先不需要这些字符:
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 formatfunction, you should have no trouble learning about the formatmethod, 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:
同时,由于您已经在使用该format函数,因此您应该可以轻松了解该format方法,这使事情变得更加简单。同样,您可以使用三个单独的语句——但在这种情况下,多行(三引号)字符串可能更容易阅读:
print("""\tOrder total ${:10.2}
\tDiscount    ${:10.2}
\tAmount Due ${:10.2}""".format(total, disc, due))
See the tutorial section on Fancier Output Formattingfor more details on all of this.
有关所有这些的更多详细信息,请参阅有关更高级输出格式的教程部分。

