Python int对象不支持项分配
我在大学二年级。我在大学读Sci,在最近的作业中遇到了一个问题。我要做一个刽子手游戏,100%喜欢他们的输出和规格。我会用数字来格式化列表,但我不知道怎么做,这对SO来说是个新鲜事。我的问题出现在街区:Python int对象不支持项分配,python,Python,我在大学二年级。我在大学读Sci,在最近的作业中遇到了一个问题。我要做一个刽子手游戏,100%喜欢他们的输出和规格。我会用数字来格式化列表,但我不知道怎么做,这对SO来说是个新鲜事。我的问题出现在街区: for i in range(0, stringSize, 1): answerStr[i] = '_' 它给了我错误 int object does not support item assignment 在其他语言中,我可以创建一个大小相同的字符串(userChosenWord)
for i in range(0, stringSize, 1):
answerStr[i] = '_'
它给了我错误
int object does not support item assignment
在其他语言中,我可以创建一个大小相同的字符串(userChosenWord),但Python的字符串库及其动态类型都有问题。在作业中,我必须将当前字符串输出为\uuuuuuuuuuuuuuuu
,如果用户要猜测e
单词horse
,我必须告诉用户迄今为止匹配的字母:\uuuuuuuue
。我希望这是有道理的
另外,如果你们中有人对我的代码有任何提示/意见,请告诉我。我一直在学习
wordList = ['cow', 'horse', 'deer', 'elephant', 'lion', 'tiger', 'baboon', 'donkey', 'fox', 'giraffe'] #will work for words <=100 chars
inputList = "abcdefghijklmnopqrstuvwxyz"
illegalInputList = "!@#$%^&*()_+-=`~;:'\"<,>.?/|\\}]{["
def game():
attemptsMade = 0
print("Welcome to Hangman. Guess the mystery word with less than 6 mistakes.")
userInputInteger = int(input("Please enter an integer number (0<=number<10) to choose the word in the list:"))
if (0 > userInputInteger or userInputInteger > 9):
print("Index is out of range.")
game()
for i in range(0, len(wordList)):
if (userInputInteger == i):
#userChosenWord is string from wordList[i]
userChosenWord = wordList[i]
print("The length of the word is:", len(userChosenWord))
break
stringSize = len(userChosenWord)
answerStr = len(userChosenWord)
#make a temp string of _'s
for i in range(0, stringSize, 1):
answerStr[i] = '_'
keyStr = userChosenWord
def play():
guessChar = input("Please enter the letter you guess:")
if guessChar not in inputList:
print("You must enter a single, alphabetic character.")
play()
if guessChar in illegalInputList:
print("Input must be an integer.")
play()
if (guessChar == ('' or ' ')):
print("Empty input.")
play()
attemptsMade += 1
if guessChar in userChosenWord:
for i in range(0, stringSize, 1):
if (keyStr[i] == guessChar):
answerStr[i] = guessChar
print("Letters matched so far: %s", answerStr)
else:
print("The letter is not in the word.")
play()
if (answerStr == userChosenWord):
print("You have guessed the word. You win. \n Goodbye.")
sys.exit()
if (attemptsMade <= 6):
play()
if (attemptsMade > 6):
print("Too many incorrect guesses. You lose. \n The word was: %s", userChosenWord)
replayBool = bool(input("Replay? Y/N"))
if (replayBool == 'y' or 'Y'):
play()
elif (replayBool == 'n' or 'N'):
print("Goodbye.")
game()
wordList=['cow'、'Mars'、'deer'、'elephant'、'lion'、'tiger'、'baboon'、'驴'、'fox'、'giraffe']#将用于单词部分答案。还有更多的事情要做,但是关于“int”对象不支持项目分配
:
您正在将answerStr
设置为一个数字len(userChosenWord)
,它是userChosenWord
的长度
但是你试着把它当作一个列表来使用。要创建长度的空列表len(userChosenWord)
do:
answerStr = [0]*len(userChosenWord)
或相当于:
answerStr = [0 for i in userChosenWord]
这里有很多错误play()
几乎肯定不应该是递归的(不应该调用自身)。您也不能在play()
中为game()
(attemptsMade
)分配一个局部变量(而且,replayBool是一个布尔值,永远不会等于“y”、“y”、“n”或“n”…)我的错误。谢谢你的解释。你能解释一下为什么play()不应该调用它自己吗?每次调用play()
完成后,调用它的那一个将继续。这几乎肯定不是你想要的。非常感谢。我认为这是一个语法错误。@Wooble,不是,因为字符串是不可变的。