我一直在第一行收到EOL错误消息

时间:2016-12-05 20:15:05

标签: python python-3.x

我正在制作一个晚餐菜单,我不知道我在第一行做错了什么。我认为这是一个意想不到的缩进"但我不知道如何纠正它。任何帮助都是极好的。

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 个答案:

答案 0 :(得分:1)

  

字符串文字可以跨越多行。一种方法是使用   三重报价:""" ..."""或'' ...'''。行尾是自动的   包含在字符串中,但可以通过添加a来防止这种情况   \在行尾。以下示例:

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

有关详细信息,请参阅here

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"''')

这将解决它

答案 1 :(得分: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))