코드 : (파이썬 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()
질문 : 난
- 사용자 목록을 문자열로 변환하여 인쇄하고 문제가 발생하여 사용자가 선택한 단어의 위치를 인쇄하는 데 문제가 있습니다. 도움이 필요한 코드는 "이 코드는 수정이 필요합니다 :"줄 사이에 있습니다. 분명히 실제 코드에는 없습니다.
사용자가 문장을 입력하는 코드를 작성하고 문장을 목록으로 변환하여 사용자에게 보여 주어 사용자가 만족하는지 확인합니다. 그 다음에 (내가 문제가있는 비트) 사용자는 문장에서 단어의 위치를 알고 싶은 단어를 선택하고 선택한 단어의 위치를 인쇄해야합니다. 나는 모든 코드를 완전히 보여 주기로 결정했기 때문에 누구나 독서를 할 수있어서 (코드 작성은 즐거웠으므로 학교 과제로 만 사용함) 전반적인 코드를 향상시킬 수있었습니다.
것 같습니다 :
여기 분할하고있는 열거 무엇을 보여 일부 콘솔 출력입니다. 한 문장으로 시작해서 단어 목록에 들어간 거지? 그러나 목록이 여러 문장으로 구성되어 있다고 생각하는 것 같습니다. – jcfollower
중복이 가능합니까? http://stackoverflow.com/questions/39625383/how-to-make-user-input-a-list-and-assign-numbers-to-each-word –