-1

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.

Potato
  • 13
  • 4
  • I see absolutely no attempt at writing to a file here. Please do research, then make an attempt. – TigerhawkT3 Oct 08 '16 at 21:05
  • What is the purpose of that complication? If you just save the sentence, it will be saved with all words and positions. – zvone Oct 08 '16 at 21:07
  • I have done research and that is why I removed my attempt from the code because it just looked messy and hard to read, I wouldn't be asking in the first place if I didn't at least ATTEMPT to research it on my own; I simply don't know the code needed to do so. – Potato Oct 08 '16 at 21:08
  • 1
    "I wouldn't be asking in the first place if I didn't at least ATTEMPT to research it on my own" - You would be surprised at how many people don't do that. :P We do need to see your attempt, even if it's messy, so that we can help resolve the actual issues you're facing rather than simply act as a coding service (which isn't what this site provides). – TigerhawkT3 Oct 08 '16 at 21:10

1 Answers1

1

Iterate over the list containing the words of sentence. For each word, check that whether it exists in the other list containing words for finding the positions. Use collections.defaultdict to store the index of each word. Below is the sample code:

from collections import defaultdict

my_sentence = 'this is my sentence where this content is supposed to be checked'
sentence_words = my_sentence.split()
my_dict = defaultdict(list)
for i, item in enumerate(sentence_words):
    my_dict[item].append(i)

# content of 'my_dict':
# {'this': [0, 5], 'is': [1, 7], 'where': [4]}

Refer Python Print String To Text File regarding how to write content to a a file.

Community
  • 1
  • 1
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • why not just `my_dict[item].append(i)` - I mean why did you use the `word_list` ?? – coder Oct 08 '16 at 21:36
  • I thought OP wants to print the position of some specific words from another list. Again went through the question but that was not the case. Updated the answer – Moinuddin Quadri Oct 08 '16 at 21:39
  • Oh, I did use your code in the end I just forgot to update my edit to your version. Thanks for the help! – Potato Oct 08 '16 at 22:30