代码:(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()
守则正在努力实现
问题
答案 0 :(得分:2)
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)
作为一个完全不相关的旁边,你应该阅读python pep8,尤其是关于变量名的一点......
答案 1 :(得分:0)
if Users_List == Chosen_Word.upper(): # <-- .upper() with Choosen_word
..something.. str((Users_List.index(Chosen_Word) +1)) # <-- without ".upper()"
在两个地方保持一致。根据您需要不区分大小写/敏感的搜索来添加/删除.upper()
。
答案 2 :(得分:0)
因此,这将在列表中找到该单词的位置,您可以轻松地为您的代码修改它。
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
请注意,这是索引,python中的索引从0开始,而不是1.如果你想要更人性化的表示,只需在计数中加一个。
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
答案 3 :(得分:0)
在这一行
print("Your word appears in the number " + str((Users_List.index(Chosen_Word) +1)) + " slot of this sentence")
你试图在句子的单词的一个中得到Chosen_Word的索引。你想要句子中的索引。问题是那个
print("Your word appears in the number " + str((Sentence_List.index(Chosen_Word) +1)) + " slot of this sentence")
当Chosen_Word在句子中出现多次时,无法正常工作。 因此,最好使用枚举:
重新构造循环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")
注意,我还删除了一个上层调用。输入单词时,您已调用 upper 。
另请注意,当单词不在句子中时,您不会得到输出。在这种情况下,您可能想要添加特殊输出。