2016-12-05 118 views
0

我在做一個小餐館菜單,我不知道我在第一行做錯了什麼。我認爲這是一個「意想不到的縮進」,但我不知道如何糾正它。任何幫助都是極好的。我一直在第一行收到EOL錯誤消息

print("Breakfast_Menu 
    (1) "Pancakes and eggs" 
    (2) "Waffles with your pick between apple or oranges" 
    (3) "Cheerios with month-old milk" 
    (4) "Sausage-Egg Sandwich with yogurt" 
    (5) "Sausage Biscuit with bacon" 
    (6) "Oatmeal and applesauce" 
    (7) "Coffee with air") 
+2

的報價是混淆了'print'聲明。嘗試在打印語句 – Jakub

回答

1

你的語法不正確:

print("Breakfast menu 

打開一個字符串,

(1) " 

仍然是它的一部分,最後"關閉它。

Pancakes and eggs 

然後解析爲Python代碼(即一個變量命名爲Pancakesand關鍵字和另一個egg變量等

你「EOF」的消息是,有號的原因。總的雙引號代碼的最後一部分實際上是開放的字符串:

") 

這是永遠不會關閉。換句話說,即使解析代碼之前,python也會到達文件末尾。得到你想要我怎麼想的

一種方法是:

menu = [ 
    "Pancakes and eggs", 
    "Waffles with your pick between apple or oranges", 
    "Cheerios with month-old milk", 
    "Sausage-Egg Sandwich with yogurt", 
    "Sausage Biscuit with bacon", 
    "Oatmeal and applesauce", 
    "Coffee with air", 
] 
print("Breakfast menu") 
for n, item in enumerate(menu): 
    print("(%s) %s" % (n + 1, item)) 
1

字符串文本可以跨越多行。一種方法是使用 三重引號:「」「...」「」或'''...'''。行末自動包含在字符串中,但可以通過在行尾添加 \來防止此行。下面的例子:

print("""\ 
Usage: thingy [OPTIONS] 
    -h      Display this usage message 
    -H hostname    Hostname to connect to 
""") 

瞭解更多信息

print('''"Breakfast_Menu" 
    (1) "Pancakes and eggs" 
    (2) "Waffles with your pick between apple or oranges" 
    (3) "Cheerios with month-old milk" 
    (4) "Sausage-Egg Sandwich with yogurt" 
    (5) "Sausage Biscuit with bacon" 
    (6) "Oatmeal and applesauce" 
    (7) "Coffee with air"''') 

here這將解決這個問題

+2

的任一端添加單引號,除了代碼之外,還會添加一個簡要說明,以使此答案更好。 –

+0

你也可以逃過引號:) –

相關問題