0

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')
2
  • 1
    The else statement corresponds only to if file_option == '2':, so even if you enter 1, you will get the results of if file_option == '1': plus the results fo the else Commented Jun 8, 2021 at 13:49
  • 2
    Your else branch only applies to the if file_option == '2' check. You should use elif instead of if there. Commented Jun 8, 2021 at 13:49

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.