Zachary Cohn
Zachary Cohn

Reputation: 11

First if statement in while loop not working

I am a beginner and I can't for the life of me find the error.

When I run this function and input '2' it continues and works properly. When I enter '3' it prints the proper message and loops back around to the top...but when I enter '1', this is the output:

Hmm... Given the options "1" and "2," you chose "1"!!!!???

try again.

The code for option 1 and option 2 are exactly the same (I copy and pasted), but the first option doesn't work. I even tried switching the values around, so that the boolean statements were flipped (the first option read, if file_option == '2' and the second read if file_option == '1') and no luck. I have determined that it is the first option that is always broken, but I do not understand why.

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')

Upvotes: 0

Views: 39

Answers (1)

Squishy
Squishy

Reputation: 96

I changed the if file_option == "2" to 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')

Upvotes: 1

Related Questions