使用 R markdown 时如何在 pdf 中输出 Python 代码?

How to fit the output of Python code in pdf while using R markdown?

我正在尝试将 R Markdown 文件编织成 PDF。我有几个 Python 代码块,其中一个的输出不适合页面。 我还阅读了有关设置 R 块宽度的信息,但遗憾的是,当使用 {python…………} 时,这些功能不起作用。 我的代码:

Marks = [23,19,14,10,23,34,15,19,24,19,2,20,30]

for Score in Marks : 
    if Score > 15 : 
        print(Score, "-", "The candidate has passed.")
    else :
        print(Score, "-", "The candidate has failed.")
 
OpenDay = ["The Score is" + " " + str(Score) + " " + "and The candidate has passed." if Score > 15 in Marks 
else "The Score is" + " " + str(Score) + " " + "and The candidate has failed." for Score in Marks]

print(OpenDay)

请帮忙! enter image description here

人们在编写代码时经常会遇到同样的问题,因为它应该完全适合 window。您可以像这样格式化 OpenDay 赋值:

OpenDay = [
    (
        "The Score is " + str(Score) 
        + " and The candidate has passed."
    ) if Score > 15 else (
        "The Score is " + str(Score) 
        + " and The candidate has failed."
    )
    for Score in Marks
]

这有一些简化和代码修复,但我可能会进一步简化它:

OpenDay = [
    "The score is {} and the candidate has {}."
    .format(Score, "passed" if Score > 15 else "failed")
    for Score in Marks
]