簡體   English   中英

有沒有辦法使用用戶在 python 中輸入的答案? (使用查詢器)

[英]Is there a way to use the answers entered by the user in python? (using inquirer)

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

我被困住了......有人可以幫忙嗎?

似乎變量answers包含一個字典。 因此, if檢查底部的內容,您必須更改您:

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

那應該具有所需的功能。

應該使用字典。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM