이것은 내 프로그램입니다.왜 이름 오류가 나타 납니까?
sentence = raw_input("Please type a sentence:")
while "." in sentence or "," in sentence or ":" in sentence or "?" in
sentence or ";" in sentence:
print("Please write another sentence without punctutation ")
sentence = input("Please write a sentence: ")
else:
words = sentence.split()
print(words)
specificword = raw_input("Please type a word to find in the sentence: ")
while i in range(len(words)):
if specificword == words[i]:
print (specificword, "found in position ", i + 1)
else:
print("Word not found in the sentence")
specificword = input("Please type another word to find in the sentence")
이 오류가 나타납니다이 프로그램을 실행 한 후, 문장을 입력하세요 : 안녕 내 이름은 제프 입니다 [ '안녕하세요', '내', '이름', '제프' '입니다']하십시오 문장에서 찾으실 단어를 입력하십시오 : jeff
Traceback (most recent call last):
File "E:/school/GCSE Computing/A453/Task 1/code test1.py", line 9, in <module>
while i in range(len(words)):
NameError: name 'i' is not defined
무엇이 잘못 되었습니까?
. while 루프에서 이렇게하면'i' 변수의 내용이'range' 함수에 의해 생성 된리스트 내에 포함되어 있는지 확인하게됩니다. 이렇게하면 'i'가 정의되지 않았으므로 오류가 발생합니다. – Shadow
정확히 그 문제는 @ 그림자입니다. 'for '루프는 새로운 변수를 정의합니다. 'while' 루프는 범위에서 조건을 평가합니다 :'i'는 이미 존재해야합니다. 그리고 의미 론적으로, '나는 범위 내에있다 (len (단어))'는 제프가 원하는 것이 아니다. –
덧붙여 Jeff는 내장 된'enumerate' 함수를 살펴 봅니다. –