2016-10-10 2 views
-1

「コーダーの見習い:Python 3で学習する」(http://www.spronck.net/pythonbook/pythonbook.pdf)を使用しています。「The」という単語が、印刷機能に構文エラーを引き起こしています - Python

「この本のカバー価格は24.95ドルですが、書店には40%の割引があります。 送料は、最初のコピーでは3ドル、追加のコピーでは75セントです。 60枚分。

これは私のコードです:何らかの理由

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に達するまで私は各単語を削除することを考えると、私はまだ構文エラーを取得します。のみforbutが残っている場合は、エラー:

EOL while scanning string literal

が生成されます。私は何をすべきかわからない。あなたは余分な"を得たのでUsing IDLE editor (not prompt).

+3

'' str(shipping_first) 'で' ''を削除してください。また、あなたは '' str.format'(https:/)を見てみたいかもしれませんが、 '' str(shipping_rest) ''の後に '' str(sixty_shipped') /docs.python.org/3/library/stdtypes.html#str.format) –

+4

Stackoverflows構文のHightlighterでもエラーが表示されます:) –

+2

引用符が一致しません。 'print'の中の文字列の書式からそれをはっきりと見ることができます –

答えて

1

は、ここに私のコードです。代わりに

(print("The first copy ships for " + "str(shipping_first) + ", but the rest ships for " + str(shipping_rest) ".")) 

(print("The first copy ships for " + str(shipping_first) + ", but the rest ships for " + str(shipping_rest) + ".")) 

を行います。また、print()ドキュメントから、str()を呼び出す省略することができるの:

All non-keyword arguments are converted to strings like str() does and written to the stream

UPD

また、あなたはの終わりに+をスキップエラーライン。 そして@tobias_kあなたはstr方法あなたのコードがstr()方法なしで動作するためにそうprint("Given 60 copies were shipped, it would cost " + str(sixty_shipped + ".")

ため)を閉じ忘れに述べたように:より良いformat()

print("The first copy ships for {}, but the rest ships for {}.".format(shipping_first, shipping_rest)) 

print("The first copy ships for ", shipping_first, ", but the rest ships for ", shipping_rest, ".") 

またはそれは今よります読める。

+0

最後の行に少なくとも1つの誤字があることに注意してください。 –

+0

@tobias_kありがとう、私はOPが(回答を更新した)すべてのタイプミスを言及したと思います。 –

関連する問題