You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pico W测试页面字符串双占位符替换报错TypeError,求正确语法

解决字符串格式化错误的方案

你的问题出在Python字符串格式化的语法上,当格式字符串包含多个%s占位符时,不能分两次用%传递参数,必须一次性传入所有参数,用元组包裹。

错误原因

你当前的代码testpanel % sentence1 % sentence2会先执行testpanel % sentence1,但testpanel里有两个%s占位符,只传一个参数sentence1会直接触发TypeError: format string needs more arguments错误——因为字符串需要2个参数但只收到1个。

正确代码

把两个参数放到元组里,一次性传递:

response = testpanel % (sentence1, sentence2)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)

额外的HTML修正建议

你的HTML字符串里还有两处小问题,可能导致页面渲染异常:

  1. 两处<p>%s<p>没有正确闭合,应该改为<p>%s</p>
  2. 字符串末尾多余的``符号需要删除,否则会输出到页面中

修正后的testpanel定义:

testpanel = """<!DOCTYPE html>
<html>
    <head> <title>tester Pico W</title> </head>
    <body> <h1>tester Pico W</h1>
        <p>%s</p>
        <p>"hello from tester"</p>
        <p>%s</p>
    </body>
</html>
"""

内容的提问来源于stack exchange,提问作者ArduinoPobs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 16:33:14