簡體   English   中英

while循環中的第一個if語句不起作用

[英]First if statement in while loop not working

我是初學者,我一輩子都找不到錯誤。

當我運行此 function 並輸入“2”時,它會繼續並正常工作。 當我輸入“3”時,它會打印正確的消息並循環回到頂部……但是當我輸入“1”時,這是 output:

嗯...給定選項“1”和“2”,您選擇了“1”???!???

再試一次。

選項 1 和選項 2 的代碼完全相同(我復制粘貼),但第一個選項不起作用。 我什至嘗試切換值,以便翻轉 boolean 語句(第一個選項讀取, if file_option == '2'和第二個讀取if file_option == '1' )並且沒有運氣。 我已經確定它是始終被破壞的第一個選項,但我不明白為什么。

def file_path(location):
    home = os.path.expanduser('~')
    download_path = os.path.join(home, location)
    return download_path

def file_path_chooser():
    clear()
    file_option = None
    while file_option not in ('1','2'):
        file_option = input('''
Choose from the following locations:
Note: You will have the option to choose folders inside of whatever you choose.

type '1' for Documents
type '2' for Desktop

response: ''')
        if file_option == '1':
            clear()
            dir_contents_list = os.listdir(file_path('Desktop'))
            folder_list = []
            p0 = '''
Please pick from the following locations:
'''
            for item in dir_contents_list:
                if '.' not in item:
                    folder_list.append(item)
                p1 =''
                for folder in folder_list:
                    folders_text = '''
type '{num}' to save the file in {folder}'''.format(folder=folder, num=folder_list.index(folder))
                    p1 += folders_text
            print(p0+p1)
        if file_option == '2':
            clear()
            dir_contents_list = os.listdir(file_path('Desktop'))
            folder_list = []
            p0 = '''
Please pick from the following locations:
'''
            for item in dir_contents_list:
                if '.' not in item:
                    folder_list.append(item)
                p1 =''
                for folder in folder_list:
                    folders_text = '''
type '{num}' to save the file in {folder}'''.format(folder=folder, num=folder_list.index(folder))
                    p1 += folders_text
            print(p0+p1)
        else:
            clear()
            print('Hmm... Given the options "1" and "2," you chose ' + '"'+ file_option.upper()+'"!!!!???\n\ntry again.')
            time.sleep(1)
                



    

    
def clear():
    # for windows
    if name == 'nt':
        _ = system('cls')
    # for mac and linux (note: for these, os.name = 'posix)
    else:
        _ = system('clear')

我將if file_option == "2"更改為elif file_option == "2"

def file_path(location):
    home = os.path.expanduser('~')
    download_path = os.path.join(home, location)
    return download_path

def file_path_chooser():
    clear()
    file_option = None
    while file_option not in ('1','2'):
        file_option = input('''
Choose from the following locations:
Note: You will have the option to choose folders inside of whatever you choose.

type '1' for Documents
type '2' for Desktop

response: ''')
        if file_option == '1':
            clear()
            dir_contents_list = os.listdir(file_path('Desktop'))
            folder_list = []
            p0 = '''
Please pick from the following locations:
'''
            for item in dir_contents_list:
                if '.' not in item:
                    folder_list.append(item)
                p1 =''
                for folder in folder_list:
                    folders_text = '''
type '{num}' to save the file in {folder}'''.format(folder=folder, num=folder_list.index(folder))
                    p1 += folders_text
            print(p0+p1)
        elif file_option == '2':
            clear()
            dir_contents_list = os.listdir(file_path('Desktop'))
            folder_list = []
            p0 = '''
Please pick from the following locations:
'''
            for item in dir_contents_list:
                if '.' not in item:
                    folder_list.append(item)
                p1 =''
                for folder in folder_list:
                    folders_text = '''
type '{num}' to save the file in {folder}'''.format(folder=folder, num=folder_list.index(folder))
                    p1 += folders_text
            print(p0+p1)
        else:
            clear()
            print('Hmm... Given the options "1" and "2," you chose ' + '"'+ file_option.upper()+'"!!!!???\n\ntry again.')
            time.sleep(1)
                



    

    
def clear():
    # for windows
    if name == 'nt':
        _ = system('cls')
    # for mac and linux (note: for these, os.name = 'posix)
    else:
        _ = system('clear')

暫無
暫無

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

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