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

如何在click.prompt的用户输入前添加$符号且无多余冒号

解决click.prompt输入前追加$符号的方案

click.prompt默认会在传入的提示文本末尾追加prompt_suffix参数的内容,该参数默认值为': ',这就是直接拼接$会出现多余冒号的原因。可以通过以下两种方式实现预期效果:

  • 方案1(最推荐):修改prompt_suffix为空字符串,自定义完整提示内容
    代码示例:

    original_price = click.prompt('Please enter the flight fare: $', type=int, prompt_suffix='')
    

    该方案运行后交互显示为:Please enter the flight fare: $1,完全符合预期。

  • 方案2:拆分提示输出与输入采集
    如果需要保留其他场景下prompt_suffix的默认逻辑,也可以拆分实现:

    # nl=False控制echo输出不换行,和后续输入保持同一行
    click.echo('Please enter the flight fare: $', nl=False)
    original_price = click.prompt('', type=int, prompt_suffix='')
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 07:57:04