제가하고있는 작업은 문장에서 개별 단어를 식별하고이를 목록에 저장하고 원래 문장의 각 단어를 목록에서 해당 단어의 위치로 대체하는 프로그램을 개발하는 것입니다.
sentencelist=[] #variable list for the sentences
word=[] #variable list for the words
positions=[]
words= open("words.txt","w")
position= open("position.txt","w")
question=input("Do you want to enter a sentence? Answers are Y or N.").upper()
if question=="Y":
sentence=input("Please enter a sentance").upper() #sets to uppercase so it's easier to read
sentencetext=sentence.isalpha or sentence.isspace()
while sentencetext==False: #if letters have not been entered
print("Only letters are allowed") #error message
sentence=input("Please enter a sentence").upper() #asks the question again
sentencetext=sentence.isalpha #checks if letters have been entered this time
word = sentence.split(' ')
for (i, check) in enumerate(word): #orders the words
print(sentence)
word = input("What word are you looking for?").upper() #asks what word they want
if (check == word):
positionofword=print("your word is in this position:", i+1)
positionofword=str(positionofword)
else:
print("this didn't work") #print error message
elif question=="N":
print("The program will now close")
else:
print("you did not enter one of the prescribed letters")
words.write(word + " ")
position.write(positionofword + " ")
나에게 문제는 내가 루프에 갇혀 있다는 것입니다.
word = input("What word are you looking for?").upper() #asks what word they want
if (check == word):
positionofword=print("your word is in this position:", i+1)
positionofword=str(positionofword)
else:
print("this didn't work") #print error message
따라서 파일에 단어를 가져올 수 없음을 의미합니다. 나는 break를 사용하려고했지만 파일에 단어를 넣을 수 없었기 때문에 그것은 나에게 적합하지 않았습니다.
저는이 사이트를 처음 접했지만 꽤 오랫동안 스토킹을했습니다. 바라건대 이것이 옳았 으면 좋겠어요. 제가 잘못 말하면 비판을들을 수 있습니다.
for 루프의 논리가 잘못되었습니다. 사용자가 찾고자하는 단어가 무엇인지 한 번 묻지 않고 문장의 각 단어에 대해 질문하고 현재 확인중인 단어 일 때 원하는 단어를 입력 한 경우에만 일치합니다. . 또한 문장의 각 단어에 대해 한 번씩 문장을 인쇄합니다. 다음과 같이 재구성하십시오.
print(sentence)
sentence_words = sentence.split(' ')
word = input("What word are you looking for?").upper() #asks what word they want
for (i, check) in enumerate(sentence_words): #orders the words
if (check == word):
print("your word is in this position:", i+1)
positionofword=i+1
break
else:
print("This didn't work")
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다