为什么字典在这个whileloop中填充,但在循环之外它是“空的”?

2024-09-24 00:35:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过收集用户关于文件夹名称和扩展名的输入,自动使用Python实现文件管理的自动化,他/她希望自动将这些输入放在该文件夹中

创建字典以将文件夹名称保存为字符串(字典键)并将扩展名保存为字符串列表后,用户将填充每个列表,并在填充字典时将两者合并

问题:我能够用上述信息填充循环内部的字典,但我已经尝试在while循环外部打印字典,但它不起作用。因此,我的程序不工作,因为字典中没有这些值您能检查一下为什么我在退出while循环后无法保留字典中的信息吗?

模拟:程序一直工作到循环的最后一部分(见下图),然后关闭窗口,什么也不发生

enter image description here

代码的第一部分:

import os

# Library that helps to find path of the items
from pathlib import Path

# Create list variables
namefiles = []
nameextension1 = []
flag = True
uanswer = ""
count = 0
count2 = 1
flag2 = True
extbreak = ""

# Create dictionaries to organize the folders
Subdirectories = {}

# This is the while-loop to collect the input from the user and populate the lists/dictionary:*

# Collecting input from the users
while flag:
    flag2 = True
    uanswer = input("\nWould you like to create a Folder in the Library?\n Please, type 'Y' for 'yes'\n or 'N' for 'no':  ")
    if uanswer == "N":
        flag = False
    elif uanswer == "Y":
        namefiles.append(input("\nType in the name of the folder:  "))
        while flag2:
            nameextension1.append(input("\nType in the name of the extension.\n EX: .pdf:  "))
            extbreak = input("\nWould you like to add another extension?\n Please, type 'Y' for 'yes'\n or 'N' for 'no':  ")
            if extbreak == "N":
                flag2 = False
    else:
        print("\nPlease try again!!!")
    # Populating the dictionary
    Subdirectories[namefiles[count]] = nameextension1
    print (Subdirectories)
    count += 1
    # Cleaning the lists to use for the next extension
    nameextension1 = []

# I don't know if this is relevant, but the following step after the while-loop is this:*

file_formats = {file_format: directory
                for directory, file_formats in Subdirectories.items()
                for file_format in file_formats}

谢谢大家的支持和支持;如果我的解释不太清楚,我很抱歉。 如果您有任何问题,请随时发表意见,我将尽可能提供详细信息:)

PS:母亲节快乐


Tags: ofthetoinfrom文件夹forinput
2条回答

我刚刚在flag = Flase之后添加了一个中断:

import os

# Library that helps to find path of the items
from pathlib import Path

# Create list variables
namefiles = []
nameextension1 = []
flag = True
uanswer = ""
count = 0
count2 = 1
flag2 = True
extbreak = ""

# Create dictionaries to organize the folders
Subdirectories = {}

# This is the while-loop to collect the input from the user and populate the lists/dictionary:*

# Collecting input from the users
while flag:
    flag2 = True
    uanswer = input("\nWould you like to create a Folder in the Library?\n Please, type 'Y' for 'yes'\n or 'N' for 'no':  ")
    if uanswer == "N":
        flag = False
        break
    elif uanswer == "Y":
        namefiles.append(input("\nType in the name of the folder:  "))
        while flag2:
            nameextension1.append(input("\nType in the name of the extension.\n EX: .pdf:  "))
            extbreak = input("\nWould you like to add another extension?\n Please, type 'Y' for 'yes'\n or 'N' for 'no':  ")
            if extbreak == "N":
                flag2 = False
    else:
        print("\nPlease try again!!!")
    # Populating the dictionary
    Subdirectories[namefiles[count]] = nameextension1
    print (Subdirectories)
    count += 1
    # Cleaning the lists to use for the next extension
    nameextension1 = []

# I don't know if this is relevant, but the following step after the while-loop is this:*

file_formats = {file_format: directory
                for directory, file_formats in Subdirectories.items()
                for file_format in file_formats}

当用户决定完成该过程时,代码给出了IndexError。我刚刚用

while True:
    ...
    if something:
        break

而且效果很好

import os

# Library that helps to find path of the items
from pathlib import Path

# Create list variables
namefiles = []
nameextension1 = []
flag = True
uanswer = ""
count = 0
count2 = 1
flag2 = True
extbreak = ""

# Create dictionaries to organize the folders
Subdirectories = {}

# This is the while-loop to collect the input from the user and populate the lists/dictionary:*

# Collecting input from the users
while True:
    uanswer = input("\nWould you like to create a Folder in the Library?\n Please, type 'Y' for 'yes'\n or 'N' for 'no':  ")
    if uanswer == "N":
        break
    elif uanswer == "Y":
        namefiles.append(input("\nType in the name of the folder:  "))
        while True:
            nameextension1.append(input("\nType in the name of the extension.\n EX: .pdf:  "))
            extbreak = input("\nWould you like to add another extension?\n Please, type 'Y' for 'yes'\n or 'N' for 'no':  ")
            if extbreak == "N":
                break
    else:
        print("\nPlease try again!!!")
    # Populating the dictionary
    Subdirectories[namefiles[count]] = nameextension1
    print (Subdirectories)
    count += 1
    # Cleaning the lists to use for the next extension
    nameextension1 = []

print("*"*50)
print(Subdirectories)

相关问题 更多 >