2016-10-10 4 views
-1

Ich verwende "Der Lehrling des Programmierers: Python mit Python 3 lernen" (http://www.spronck.net/pythonbook/pythonbook.pdf).Das Wort "der" verursacht Syntaxfehler in der Druckfunktion - Python

Ich mache diese Übung: "Der Buchpreis beträgt 24,95 $, aber Buchhandlungen erhalten 40% Rabatt. Versandkosten $ 3 für die erste Kopie und 75 Cent für jede weitere Kopie. Berechnen Sie die gesamten Großhandel Kosten für 60 Kopien. "

Dies ist mein Code:

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 + ".") 

Aus welchem ​​Grund auch immer, das Wort the in dieser Codezeile:

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

Erzeugt einen Syntaxfehler. Da ich jedes Wort entferne, bis ich for erreiche, erhalte ich immer noch einen Syntaxfehler. Wenn nur for und but gelassen werden, der Fehler:

EOL while scanning string literal

erzeugt. Ich habe keine Ahnung, was ich tun soll.

Hier ist mein Code: Using IDLE editor (not prompt).

+3

Entfernen Sie das '' 'in' "str (shipping_first)". Außerdem fehlen ')' in 'str (sechzig_versandt' und fehlende' + 'nach' str (shipping_rest) '. Schließen für Tippfehler. Vielleicht möchten Sie auch [' str.format'] (https://docs.python.org/3/library/stdtypes.html#str.format). –

+4

Auch stackoverflows Syntax hightlighter zeigt Ihren Fehler :) –

+2

Ihre Angebote stimmen nicht überein. Das sieht man deutlich an der Formatierung der Strings in Ihrem 'print' –

Antwort

1

Weil Sie eine zusätzliche " bekam. Statt

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

tun

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

Sie auch str(), von print() docs weglassen Aufruf:

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

UPD

auch übersprungen Sie + am Ende Error Linie. Und wie @tobias_k erwähnt Sie print("Given 60 copies were shipped, it would cost " + str(sixty_shipped + ".")

Also für Ihren Code zu arbeiten, ohne str() Methoden ) für str Methode forgot endet:

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

Oder besser noch mit format()

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

Es ist jetzt mehr lesbar.

+0

Beachten Sie, dass es in der letzten Zeile noch mindestens einen Tippfehler gibt. –

+0

@tobias_k danke, ich glaube, ich erwähnte alle Tippfehler OP bekam (aktualisierte Antwort). –

Verwandte Themen