2016-09-22 5 views
0

코드 : (파이썬 3.5.2)는 문자열에서 단어의 위치를 ​​찾는

import time 
import sys 


def Word_Position_Finder(): 
    Chosen_Sentence = input("Make a simple 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": 
     print(Chosen_Sentence + ". This is your sentence.") 
     Chosen_Word = input("Which word in your sentence do you want to find the position of? ") 
     for index, word in enumerate(Sentence_List): 
      if(word == Chosen_Word): 
       print("Your word appears in the number " + str(index) + " slot of this sentence") 
     elif Chosen_Word not in Users_Sentence: 
      print("That word isn't in the sentence") 
      Choose_To_Restart() 
    else: 
     print("Restarting Program.") 
     time.sleep(1) 
     Restarting_Program() 

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("That isn't a valid answer, going to assume you said no.") 
     time.sleep(1) 
     sys.exit("Program Ended") 


def Restarting_Program(): 
    Word_Position_Finder() 


Word_Position_Finder() 

질문 : 난

  • 사용자 목록을 문자열로 변환하여 인쇄하고 문제가 발생하여 사용자가 선택한 단어의 위치를 ​​인쇄하는 데 문제가 있습니다. 도움이 필요한 코드는 "이 코드는 수정이 필요합니다 :"줄 사이에 있습니다. 분명히 실제 코드에는 없습니다.

사용자가 문장을 입력하는 코드를 작성하고 문장을 목록으로 변환하여 사용자에게 보여 주어 사용자가 만족하는지 확인합니다. 그 다음에 (내가 문제가있는 비트) 사용자는 문장에서 단어의 위치를 ​​알고 싶은 단어를 선택하고 선택한 단어의 위치를 ​​인쇄해야합니다. 나는 모든 코드를 완전히 보여 주기로 결정했기 때문에 누구나 독서를 할 수있어서 (코드 작성은 즐거웠으므로 학교 과제로 만 사용함) 전반적인 코드를 향상시킬 수있었습니다.

+1

것 같습니다 :

words_list = Users_Sentence.split() for index, word in enumerate(words_list): if(word == Chosen_Word): print("Your word appears in the number " + str(index) + " slot of this sentence") 

여기 분할하고있는 열거 무엇을 보여 일부 콘솔 출력입니다. 한 문장으로 시작해서 단어 목록에 들어간 거지? 그러나 목록이 여러 문장으로 구성되어 있다고 생각하는 것 같습니다. – jcfollower

+0

중복이 가능합니까? http://stackoverflow.com/questions/39625383/how-to-make-user-input-a-list-and-assign-numbers-to-each-word –

답변

1

어때요? 당신이 문장과 단어를 혼합하는 것처럼

>>> a="This is an example sentence" 
    >>> words = a.split() 
    >>> print(words) 
    ['This', 'is', 'an', 'example', 'sentence'] 
    >>> for i, word in enumerate(words): 
    ...  print(i) 
    ...  print(word) 
    ... 
    0 
    This 
    1 
    is 
    2 
    an 
    3 
    example 
    4 
    sentence 
+0

Ok. 나는 코드에서 분명한 실수를 수정했다. 이제는이 한 줄의 코드에만 문제가있다.'print ("당신의 단어는"+ str (Sentence_List.index (Chosen_Word) +1) + "슬롯에 나타난다. 이 문장은 ")'. 분명히 인덱스가 항상 0에서 시작하고 0 슬롯에 단어가 표시된다고 말할 수 없기 때문에 +1을 싫어합니다. 문제는 사용자가 선택한 문장에서 첫 단어 만 찾을 수 있다는 것입니다. 나는 첫 번째 위치를 지나서 단어를 찾으려고 노력한다. 내 코드는 그 단어가 존재하지 않는다고 나에게 내가 무엇을 잘못하고 있는지 정말로 모르겠다 고 말할 것이다. – Potato

+0

위의 내 솔루션을 사용한다면'print (" 당신의 단어는이 문장의 "+ str (index) +"슬롯에 나타납니다. ")' 그리고 인덱스가 1부터 시작하기를 원한다면'str (index + 1) '이됩니다 –

+0

나는 @ 위의, 어쩌면 나는 뭔가 명백한 것을 놓치고있다. 그러나 그것은 정말로 나에게 문제를 일으키는 세 줄이다. – Potato

관련 문제