Reputation: 13
Code: (Python 3.5.2)
import time
import sys
def Word_Position_Finder():
Chosen_Sentence = input("Make a simple sentence: ").upper()
print(Chosen_Sentence)
Sentence_List = Chosen_Sentence.split()
if len(Chosen_Sentence) == 0:
print("Your Sentence has no words! Restarting Program.")
time.sleep(1)
Restarting_Program()
print(Sentence_List)
time.sleep(1)
Users_Choice = input("Do you want to make a new sentence (press 1) or keep current sentence (press 2): ")
if Users_Choice == "1":
print("Restarting Program.")
time.sleep(1)
Restarting_Program()
elif Users_Choice == "2":
#Lines Under Here Don't Work As Wanted
print("'" + Chosen_Sentence + "'" + ". This is your sentence.")
Chosen_Word = input("Which word in your sentence do you want to find the position of? ").upper()
for Users_List in Sentence_List:
if Users_List == Chosen_Word.upper():
print("Your word appears in the number " + str((Users_List.index(Chosen_Word) +1)) + " slot of this sentence")
#Lines Above Here Don't Work As Wanted
else:
print("That isn't a valid answer")
Choose_To_Restart()
def Choose_To_Restart():
time.sleep(1)
loop = input("Want to try again, Y/N?")
if loop.upper() == "Y" or loop.upper() == "YES":
print("Restarting Program")
time.sleep(1)
Restarting_Program()
elif loop.upper() == "N" or loop.upper() == "NO":
print("Ending Program")
time.sleep(1)
sys.exit("Program Ended")
else:
print("Ok.")
time.sleep(1)
sys.exit("Program Ended")
def Restarting_Program():
Word_Position_Finder()
Word_Position_Finder()
What The Code Is Trying To Achieve
The Question
Upvotes: 1
Views: 182
Reputation: 891
In this line
print("Your word appears in the number " + str((Users_List.index(Chosen_Word) +1)) + " slot of this sentence")
you are trying to get the index of the Chosen_Word in one of the words of the sentence. You want the index in the sentence. Problem is that
print("Your word appears in the number " + str((Sentence_List.index(Chosen_Word) +1)) + " slot of this sentence")
isn't working either when Chosen_Word appears more than once in the sentence. So it is better to reformulate the loop with enumerate:
for i, Users_List in enumerate(Sentence_List):
if Users_List == Chosen_Word:
print("Your word appears in the number " + str(i+1) + " slot of this sentence")
Note, that I also removed one upper call. You already called upper when the word is input.
Also note, that you do not get an output when the word is not in the sentence. You may want to add a special output in this case.
Upvotes: 0
Reputation: 990
So this will find the position of the word in a list, you can easily modify it for your code.
Users_List = ['foo', 'boo', 'myword', 'fdjasi']
Chosen_Word = 'myword'
word_index = 0
for word in Users_List:
if word == Chosen_Word:
print("Your word appears in the number " + str((word_index)) + " slot of this sentence")
else:
word_index += 1
Note that this is the index, and indexes in python start from 0, not 1. If you wanted a more human representation, just add one to the count like this.
Users_List = ['foo', 'boo', 'myword', 'fdjasi']
Chosen_Word = 'myword'
word_index = 0
for word in Users_List:
if word == Chosen_Word:
print("Your word appears in the number " + str((word_index + 1)) + " slot of this sentence")
else:
word_index += 1
Upvotes: 0
Reputation: 48077
if Users_List == Chosen_Word.upper(): # <-- .upper() with Choosen_word
..something.. str((Users_List.index(Chosen_Word) +1)) # <-- without ".upper()"
Make it consistent at both the places. Add/remove .upper()
based on you want case-insensitive/sensitive search.
Upvotes: 0
Reputation: 113988
normalized_list = [word.upper() for word in Sentence_List]
try:
index= normalized_list.index(Chosen_Word.upper())
except:
print "Not Found! %s NOT in %s"%(Chosen_Word,Sentence_List)
else:
print "%s @ %s"%(Chosen_Word, index)
as a totally unrelated aside you should read the python pep8 especially the bit about variable names ...
Upvotes: 2