简体   繁体   中英

How to get two answers

the point of my program is to tell the user where the position of the word he entered is for example, ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY The word 'COUNTRY' occurs in the 5th and 17th positions.

My program only prints the first position twice. I was hoping I could get some help.

 Sentence = "the quick brown fox jumped over the lazy dog"
print (Sentence)
Sentence = Sentence.split()
while True:
    findword = input("Please enter the word to find: ")
    if not findword.isalpha() or len (findword)<3:
        print("Invalid") 
    break

for x in Sentence:
    if x==findword:
        Position = Sentence.index(findword)
        print(Position)

Try this...

s.index(x) - index of the first occurrence of x in s

Sentence = "the quick brown fox jumped over the lazy dog"
print (Sentence)
Sentence = Sentence.split()
i=0
while True:
    findword = input("Please enter the word to find: ")
    if not findword.isalpha() or len (findword)<3:
        print("Invalid") 
    break

for x in Sentence:

    if x==findword:
        Position = Sentence.index(findword, i)
        print(Position)
    i=i+1;

In index you need to specify the starting index to start the search, otherwise it will always return the first matched index.

prevPosition = 0

for x in Sentence:
    if x==findword:
        Position = Sentence.index(findword, prevPosition)
        prevPosition = Position + 1
        print(Position)

Example:

>>> Sentence
['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
>>> findword = "the"
>>> prevPosition = 0
>>> 

>>> for x in Sentence:
...     if x==findword:
...         Position = Sentence.index(findword, prevPosition)
...         prevPosition = Position + 1
...         print(Position)
... 
0
6

Here's a solution that matches your input and expected output (5th and 17th positions)

Sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
print(Sentence)
Sentence = Sentence.split()
while True:
    findword = input("Please enter the word to find: ")
    if not findword.isalpha() or len(findword) < 3:
        print("Invalid")
    break

curr_position = 0

for x in Sentence:
    if x == findword:
        Position = Sentence.index(findword, curr_position + 1)
        curr_position = Position
        print(Position + 1)

here is the change of for loop

for x in range(len(Sentence)):
    if Sentence[x]==findword:
        Position = x
        print(Position)

Try this code -

Sentence = "the quick brown fox jumped over the lazy dog"
Sentence = Sentence.split()
print (Sentence)

findword = input("Please enter the word to find: ")
if not findword.isalpha() or len (findword)<3:
    print("Invalid") 

for wordIndex, x in enumerate(Sentence):
    if x == findword:
        print(wordIndex)

Remove the while True loop during input. You are breaking after first iteration anyway. enumerate() will return index of the element along with the element when used in loop. That way you can forget about calling index()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM