我正在使用“编码员的学徒:使用Python 3学习Python”(http://www.spronck.net/pythonbook/pythonbook.pdf)。
我正在做这个练习:“一本书的封面价格是24.95美元,但书店可以获得40%的折扣。 第一份副本的运费为3美元,每份额外的副本为75美分。计算60份的批发总成本。“
这是我的代码:
jconsole
无论出于何种原因,这行代码中的单词book_price = 24.95
book_discount = book_price / 10 * 4
bookstore_book_price = book_price - book_discount
shipping_first = 3
shipping_rest = 0.75
sixty_shipped = bookstore_book_price + shipping_first + (shipping_rest * 59)
print("A book is being sold regularly for " +str(book_price) + ".")
print("At bookstores, it's being sold with a 40% discount, amounting to " + str(book_discount) + ".")
print("This means it's being sold at bookstores for " + str(bookstore_book_price) + ".")
print("The first copy ships for " + "str(shipping_first) + ", but the rest ships for " + str(shipping_rest) ".")
print("Given 60 copies were shipped, it would cost " + str(sixty_shipped + ".")
:
the
产生语法错误。鉴于我删除了每个单词,直到我到达(print("The first copy ships for " + "str(shipping_first) + ", but the rest ships for " + str(shipping_rest) "."))`
,我仍然会收到语法错误。如果只剩下for
和for
,则会出现错误:
扫描字符串文字时的EOL
产生了。我不知道该怎么做。
答案 0 :(得分:1)
因为你有额外的"
。而不是
(print("The first copy ships for " + "str(shipping_first) + ", but the rest ships for " + str(shipping_rest) "."))
DO
(print("The first copy ships for " + str(shipping_first) + ", but the rest ships for " + str(shipping_rest) + "."))
您还可以从print()
docs
str()
所有非关键字参数都转换为字符串,如str(),并写入流
<强> UPD 强>
您还在错误行末尾跳过了+
。
正如@tobias_k提到的那样,您忘记了)
方法str
print("Given 60 copies were shipped, it would cost " + str(sixty_shipped + ".")
因此,您的代码可以在没有str()
方法的情况下工作:
print("The first copy ships for ", shipping_first, ", but the rest ships for ", shipping_rest, ".")
或者更好format()
print("The first copy ships for {}, but the rest ships for {}.".format(shipping_first, shipping_rest))
它现在更具可读性。