每当用户输入时,都会遇到超出范围的错误
Whenever User Inputs, hits an Out Of Range Error
userInput = input();
for x in range(0,len(userInput)):
if(userInput[x] == " "):
spaceValue = x;
rawWord = userInput[0:spaceValue];
secWord = userInput[spaceValue+1:];
wordArray = [];
repeats = len(rawWord); #NEW VAR
if (len(rawWord) == len(secWord)):
for x in range(0,len(rawWord)):
wordArray.append(secWord[x]);
for x in range(0,len(rawWord)):
for z in range(0,len(rawWord)):
if((rawWord[x] == wordArray[z])): #Line 15 #repeats insted of wordArray[z]
wordArray.remove(rawWord[x]);
repeats = repeats - 1;
break;
if(len(wordArray) == 0):
print("YES");
else:
print("NO");
else:
print("NO");
如果 2 个单词长度相同且字母相同,代码应该打印 YES,否则打印 NO。
第 15 行出现错误: if((rawWord[x] == wordArray[z])):
IndexError: 列表索引超出范围
It works when
- 单词长度相同且字母相同。
- 单词长度不同。
- 单词长度相同并且所有字母都不同。
It doesn't work when
- 单词长度相同且不同字母但至少有一个字母相同
正如我在评论中指出的那样,您正在循环 for z in range(0,len(rawWord)):
但索引到 wordArray
中,您从循环中删除了一些东西。
如果要删除正在循环播放的内容,请务必担心。
我可以推荐一个 better/more pythonic 解决方案吗?
from itertools import permutations
userInput = input()
words = userInput.split(' ')
rawWord = words[0]
otherWords = [''.join(p) for p in permutations(words[1])]
if rawWord in otherWords:
print("YES")
else:
print("NO")
我们可以让你的工作成功 - 当你在第 15 行索引到 wordArray
时,不要忘记使用你的重复计数。或者第 15 行是什么。
我还删除了一些不需要的标点符号。
userInput = input()
for x in range(0,len(userInput)):
if userInput[x] == " ":
spaceValue = x
rawWord = userInput[0:spaceValue]
secWord = userInput[spaceValue+1:]
wordArray = [];
repeats = len(rawWord)
if len(rawWord) == len(secWord):
for x in range(0,len(rawWord)):
wordArray.append(secWord[x])
for x in range(0,len(rawWord)):
for z in range(0,len(rawWord)):
if (rawWord[x] == wordArray[z-repeats]): #Line 15
wordArray.remove(rawWord[x])
repeats = repeats - 1
break;
if(len(wordArray) == 0):
print("YES")
else:
print("NO")
else:
print("NO")
userInput = input();
for x in range(0,len(userInput)):
if(userInput[x] == " "):
spaceValue = x;
rawWord = userInput[0:spaceValue];
secWord = userInput[spaceValue+1:];
wordArray = [];
repeats = len(rawWord); #NEW VAR
if (len(rawWord) == len(secWord)):
for x in range(0,len(rawWord)):
wordArray.append(secWord[x]);
for x in range(0,len(rawWord)):
for z in range(0,len(rawWord)):
if((rawWord[x] == wordArray[z])): #Line 15 #repeats insted of wordArray[z]
wordArray.remove(rawWord[x]);
repeats = repeats - 1;
break;
if(len(wordArray) == 0):
print("YES");
else:
print("NO");
else:
print("NO");
如果 2 个单词长度相同且字母相同,代码应该打印 YES,否则打印 NO。
第 15 行出现错误: if((rawWord[x] == wordArray[z])):
IndexError: 列表索引超出范围
It works when
- 单词长度相同且字母相同。
- 单词长度不同。
- 单词长度相同并且所有字母都不同。
It doesn't work when
- 单词长度相同且不同字母但至少有一个字母相同
正如我在评论中指出的那样,您正在循环 for z in range(0,len(rawWord)):
但索引到 wordArray
中,您从循环中删除了一些东西。
如果要删除正在循环播放的内容,请务必担心。
我可以推荐一个 better/more pythonic 解决方案吗?
from itertools import permutations
userInput = input()
words = userInput.split(' ')
rawWord = words[0]
otherWords = [''.join(p) for p in permutations(words[1])]
if rawWord in otherWords:
print("YES")
else:
print("NO")
我们可以让你的工作成功 - 当你在第 15 行索引到 wordArray
时,不要忘记使用你的重复计数。或者第 15 行是什么。
我还删除了一些不需要的标点符号。
userInput = input()
for x in range(0,len(userInput)):
if userInput[x] == " ":
spaceValue = x
rawWord = userInput[0:spaceValue]
secWord = userInput[spaceValue+1:]
wordArray = [];
repeats = len(rawWord)
if len(rawWord) == len(secWord):
for x in range(0,len(rawWord)):
wordArray.append(secWord[x])
for x in range(0,len(rawWord)):
for z in range(0,len(rawWord)):
if (rawWord[x] == wordArray[z-repeats]): #Line 15
wordArray.remove(rawWord[x])
repeats = repeats - 1
break;
if(len(wordArray) == 0):
print("YES")
else:
print("NO")
else:
print("NO")