My Code: (Python: v3.5.2)
import time
import sys
def Word_Position_Finder():
chosen_sentence = input("Make a simple sentence: ").upper()
print("")
print(chosen_sentence)
print("")
sentence_list = chosen_sentence.split()
if len(chosen_sentence) == 0:
print("Your sentence has no words.")
time.sleep(1)
Choose_To_Restart()
print(sentence_list)
print("")
time.sleep(1)
users_choice = input("Press '1' to make a new sentence or press '2' keep current sentence: ")
print("")
if users_choice == "1":
print("Restarting Program.")
time.sleep(1)
Restarting_Program()
elif users_choice == "2":
print("")
print("'" + chosen_sentence + "'" + " --- " + "Is your current sentence.")
print("")
time.sleep(1)
position = []
for word in sentence_list:
postions = position.append(sentence_list.index(word) + 1)
print(position)
with open("Positions.txt", "w") as text_file:
print(chosen_sentence, position)
else:
print("That isn't a valid answer")
Choose_To_Restart()
What the code is trying to achieve:
The code above takes a sentence of the users choice, makes that sentence into a list and then prints the position of each of those words in that list and then is meant to save the user's sentence and the positions of the user's sentence into a simple .txt file.
Question:
How do I make my code create .txt file that saves the user's sentence and the positions of each word in the user's sentence into that .txt file?
The current problem I am having with the code is that the code does create a .txt file but nothing is saved in that file.