someonerandom
someonerandom

Reputation: 1

How can I prompt someone continuously with a while loop, and get out of it?

I currently have this code, and essentially when I first input something with the correct file name, for example apple.txt it will print hi and work correctly. when I don't print the correct ending, for example, apple.jpeg, it does what I want to and prompts me to try again. However, when I try again and input the correct file name ending, it doesn't recognize that I've included the correct file name ending (.txt) and it prompts me to try again. How can I fix this? Sorry if the title is kind of misleading, i struggle with english.


   filename = input("Input the Filename: ")
    f_extns = filename.split(".")
    while not filename.lower().endswith('txt'):
        input('Invalid filename extension. Please re-enter the input filename: ')
    else:
        print('hi')  

Upvotes: 0

Views: 29

Answers (1)

Dynamia
Dynamia

Reputation: 75

The issue in your code is that you don't update the variable filename, try changing this line:

input('Invalid filename extension. Please re-enter the input filename: ')

to this:

filename = input('Invalid filename extension. Please re-enter the input filename: ')

Upvotes: 2

Related Questions