如何在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
相关产品推荐
相关产品推荐

