逐行高亮显示代码
时间: 2023-07-10 21:12:47 浏览: 92
好的,我可以为您提供一个Python的示例代码,演示如何逐行高亮显示代码:
```python
import time
def countdown(n: int):
"""
Count down from n to 0 and print each number with a delay of 1 second.
"""
for i in range(n, -1, -1):
print(f"Countdown: {i}")
time.sleep(1)
countdown(5)
```
以上是一个简单的倒计时程序,当您运行它时,它将从5开始倒数到0,并且每秒钟会打印出当前倒计时的数字。
如果您想要逐行高亮显示这段代码,您可以使用类似下面的HTML和CSS代码:
```html
<pre>
<code class="python">
<span class="line">import time</span>
<span class="line">def countdown(n: int):</span>
<span class="line"> """</span>
<span class="line"> Count down from n to 0 and print each number with a delay of 1 second.</span>
<span class="line"> """</span>
<span class="line"> for i in range(n, -1, -1):</span>
<span class="line"> print(f"Countdown: {i}")</span>
<span class="line"> time.sleep(1)</span>
<span class="line">countdown(5)</span>
</code>
</pre>
<style>
.line {
display: block;
padding: 0.5em;
}
.python {
color: #000000;
background-color: #f8f8f8;
font-family: "Consolas", "Courier New", monospace;
font-size: 14px;
line-height: 1.5em;
overflow-x: auto;
padding: 1em;
}
</style>
```
这段HTML和CSS代码将会逐行高亮显示前面的Python代码。具体实现方式是使用`<pre>`和`<code>`标签来包裹代码,然后使用CSS来定义每一行的样式。
阅读全文