I want to use a while loop that will give the user an opportunity to open a new text file at the end of the program. Here is the code I have:
run_again = "yes"
run_again = run_again.upper().strip()
while run_again == "yes":
open_file = input("Please enter name of file: ")
file_name = open(open_file,"r")
for line in file_name:
line = line.strip()
rows = line.split(" ")
num = rows[0]
print(num)
run_again = input("Would you like to open a new file (yes or no)? ")
if run_again != "yes":
print("Have a great day!")
I've managed to make a while loop work with other code but I can't get it to work with opening text files.
run_again = run_again.upper().strip()makesrun_againequal toYES. Then you dowhile run_again == "yes":which is false right off the bat..upper().strip()lines inside the loop. You don't need theifstatement in the loop (you are already testing with thewhile). You should close the file when you are done reading from it.run_againtoTrue.. would be a lot cleaner