拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 有没有办法使用用户在python中输入的答案?(使用查询器)

有没有办法使用用户在python中输入的答案?(使用查询器)

白鹭 - 2022-03-25 1994 0 0

python初学者在这里。我正在尝试创建一个脚本,该脚本根据设定的规则(没有空格、数字、没有特殊字符......)递回地重命名档案夹中的档案。为此,我通过在串列中选择(全部使用查询器)来询问用户他们想要如何重命名他们的档案。问题是我不能将用户的答案与询问者一起使用。这是代码:

import os
import re
import inquirer

def space():
    for file in os.listdir("."):
        filewspace = file.replace(" ","")
        if(filewspace != file):
            os.rename(file,filewspace)

def numbers():
    for file in os.listdir("."):
        os.rename(file, file.translate(str.maketrans('','','0123456789')))

questions = [
  inquirer.List('choices',
                message="How do you want to format?",
                choices=['Remove spaces', 'Remove numbers',
                'Remove specials',],
            ),
]
answers = inquirer.prompt(questions)

if (answers) == "space":
    space()
elif (answers) == "numbers":
    numbers()
#else:
    #specials()

当我尝试在用户选择后打印“答案”中的内容时,我得到了回复: https ://i.stack.imgur.com/UrraB.jpg

我被困住了......有人可以帮忙吗?

uj5u.com热心网友回复:

该变量似乎answers包含字典。因此,您必须if在底部更改检查:

if answers["choices"] == "Remove spaces":
    space()
elif answers["choices"] == "Remove numbers":
    numbers()
...

那应该具有所需的功能。

uj5u.com热心网友回复:

应该使用字典。

if (answers['choices'] == "Remove spaces"):
    space()
elif (answers['choices'] == "Remove numbers"):
    numbers()
#else:
    #specials()
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *