Python入门者遇inquirer库问题:无法选药品且RDV重复显示
解决Python inquirer列表选择异常问题
问题根源及修复方案:
- 避免使用内置关键字作为变量名:你用了
list作为变量名,这是Python内置的列表类型,会覆盖原内置函数,导致inquirer组件调用异常。把变量名改成非关键字,比如prompt_list。 - 检查并升级inquirer版本:旧版本的inquirer可能存在List组件的渲染bug,执行命令升级:
pip install --upgrade inquirer - 调整choices参数格式:虽然元组格式合法,但换成列表格式能避免部分兼容性问题,将
choices=("RDV", "MOL", "TCZ", "DEX")改为choices=["RDV", "MOL", "TCZ", "DEX"]。
修正后的完整代码:
import inquirer prompt_list = [inquirer.List( "Medicine", message="Choose from the given list", choices=["RDV", "MOL", "TCZ", "DEX"], ),] answer = inquirer.prompt(prompt_list) print(answer["Medicine"])
内容的提问来源于stack exchange,提问作者COV
相关产品推荐
相关产品推荐

