我在这一行的连续字符错误后得到一个意外字符
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 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()
Ashwini的回答解释了为什么你的代码会给出错误。
但有一个简单得多的方法。不要像这样打印由逗号分隔的字符串,只需将这些字符串放在一起:
(我还修复了所有内容,使之适合80列屏幕,这是一个标准,一方面,它实际上在StackOverflow之类的东西上是可读的;另一方面,它使您的代码实际上并没有按照您希望的方式排列……)
在这种情况下,最好使用三个单独的
print
调用。那么您首先不需要那些\n
字符:同时,由于您已经在使用
format
函数,因此学习format
方法时应该不会遇到任何问题,这会使事情更加简单。同样,您可以使用三个单独的语句,但在这种情况下,可能更容易读取多行(三重引号)字符串:有关所有这些的详细信息,请参阅Fancier Output Formatting教程部分。
你忘了在这行的许多项目上使用引号:
另一种格式化方法是使用
str.format
如下:相关问题 更多 >
编程相关推荐