所以我有这个刽子手代码...再次,在此先感谢,它一个接一个地显示单词并且工作正常,但是当有多个单词时,例如两个l' s中的hello和两个p&# 39;在苹果中它只显示一个而不是两个。我不知道为什么会这样,而且我已经做了一些谷歌润滑,所得到的是枚举,但我已经尝试过无济于事。 这是代码:
def hangman():
global fullWordList
fullWordList = []
global wrongnums
wrongnums =[]
print("What is the word to be guessed?")
print("Words means that there are no numbers")
global guessnum
guessnum=input('>')
fullWord = guessnum
for line in fullWord:
for c in line:
fullWordList.append(c)
print (fullWordList)
print("how many chances?")
global chances
chances=int(input('>'))
print("game in beginning")
# sleep(5)
# print ("\n" * 100)
global blanks
blanks = '_ ' * len(guessnum)
print()
print(blanks)
guessing()
def guessing():
global fullWordList
global chances
print("guess a letter")
global guess
guess=input('>')
guessloop()
def guessloop():
global chances
if guess in fullWordList:
if guess in fullWordList:
letterIndex = guessnum.index(guess)
global blanks
blanks = blanks[:letterIndex*2] + guess + blanks[letterIndex*2+1:]
fullWordList.remove(guess);
global lenWord
lenWord = len(fullWordList)
print (fullWordList)
print (lenWord)
print ("Guess is correct!")
if guess in fullWordList:
guessloop()
else:
wrongnums.append(guess)
print (wrongnums)
if lenWord == 0:
print()
print("Word: ",blanks)
win()
else:
print()
print("Word: ",blanks)
guessing()
else:
if lenWord == 0:
win()
else:
print()
print("Word: ",blanks)
guessing()
elif guess in wrongnums:
print("You've guessed that!")
guessing()
else:
chances -=1
wrongnums.append(guess)
print (wrongnums)
print ("Guess is wrong! ", chances, " more failed attempts allowed.")
if chances == 0:
gameOver()
else:
guessing()
def check():
print()
print("Word: ",newBlanks)
guessing2()
def win():
print("You guessed it well done")
print("Would you like to play again?")
PG = input(">")
if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
print ("Game will restart in 5 seconds")
sleep(5)
hangman()
else:
print("Going to main menu in 5 seconds")
sleep(5)
start()
def gameOver():
print("You ran out of guesses, sorry, you lose")
print("Would you like to play again?")
PG = input(">")
if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
print ("Game will restart in 5 seconds")
sleep(5)
hangman()
else:
print("Going to main menu in 5 seconds")
sleep(5)
start()
现在我也有了一个代码:
def hangman():
global fullWordList
fullWordList = []
global wrongnums
wrongnums =[]
print("What is the word to be guessed?")
print("Words means that there are no numbers")
global guessnum
guessnum=input('>')
fullWord = guessnum
for line in fullWord:
for c in line:
fullWordList.append(c)
print (fullWordList)
print("how many chances?")
global chances
chances=int(input('>'))
print("game in beginning")
# sleep(5)
# print ("\n" * 100)
global blanks
blanks = '_ ' * len(guessnum)
print()
print(blanks)
guessing()
def guessing():
global fullWordList
global chances
print("guess a letter")
global guess
guess=input('>')
guessloop()
def guessloop():
global chances
if guess in fullWordList:
if guess in fullWordList:
letterIndex = guessnum.index(guess)
global blanks
guesses = (guessnum)
guesses = blanks[letterIndex]
blanks[letterIndex] = guess
fullWordList.remove(guess);
global lenWord
lenWord = len(fullWordList)
print (fullWordList)
print (lenWord)
print ("Guess is correct!")
if guess in fullWordList:
guessloop()
else:
wrongnums.append(guess)
print (wrongnums)
if lenWord == 0:
print()
print("Word: ",blanks)
win()
else:
print()
print("Word: ",blanks)
guessing()
else:
if lenWord == 0:
win()
else:
print()
print("Word: ",blanks)
guessing()
elif guess in wrongnums:
print("You've guessed that!")
guessing()
else:
chances -=1
wrongnums.append(guess)
print (wrongnums)
print ("Guess is wrong! ", chances, " more failed attempts allowed.")
if chances == 0:
gameOver()
else:
guessing()
def check():
print()
print("Word: ",newBlanks)
guessing2()
def win():
print("You guessed it well done")
print("Would you like to play again?")
PG = input(">")
if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
print ("Game will restart in 5 seconds")
sleep(5)
hangman()
else:
print("Going to main menu in 5 seconds")
sleep(5)
start()
def gameOver():
print("You ran out of guesses, sorry, you lose")
print("Would you like to play again?")
PG = input(">")
if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
print ("Game will restart in 5 seconds")
sleep(5)
hangman()
else:
print("Going to main menu in 5 seconds")
sleep(5)
start()
hangman()
这是我编辑它并得到这个错误:
blanks[letterIndex] = guess
TypeError: 'str' object does not support item assignment
我真的很难过,我希望这有足够的数据来帮助。
答案 0 :(得分:1)
我尝试尽可能少地更改您的代码,但我需要让代码完全运行以解决所有错误,所以我正在分享我对您的代码所做的事情。
你已经确定了两个问题 - 一个是只要有一个匹配,程序就会继续下一个猜测,而不会检查多个正面命中。
第二个问题是你得到的错误是'blanks'是一个字符串,你试图更改特定索引处的字符(第二个代码示例的第43行:blanks[letterIndex] = guess
)。字符串是不可变的,因此无法尝试更改字符。围绕这个的方法不止一种 - 我跟(下面)一起将'空白'设置为列表,允许您更改列表中的任何元素(因为列表是可变的)。要向用户显示列表,请尝试''.join(blanks)
从列表中生成可显示的字符串。
我对代码进行了一些更改(再次,尝试尊重您的结构和样式)以提出有效的方法:
仍然没有例外,但是这里有什么用'苹果'这样的词 - 试图赢,输,重复猜测。
希望它有所帮助 - 让我知道是否有一些不明确的事情。我不想只是重新编写代码,而是希望通过一种方法解决问题。
还可以进行其他改进,但这至少可以解决你的障碍。
代码
def hangman():
global fullWordList
fullWordList = []
global wrongnums
wrongnums =[]
print("What is the word to be guessed?")
print("Words means that there are no numbers")
# no need to set two variables for same thing (fullWord and guessnum)
global fullWord
fullWord=input('>')
for line in fullWord:
for c in line:
fullWordList.append(c)
print (fullWordList)
print("how many chances?")
global chances
chances=int(input('>'))
print("game in beginning")
# sleep(5)
# print ("\n" * 100)
global blanks
# treat blanks as a list rather than string (list is mutable)
blanks = ['- '] * len(fullWord)
print()
print(blanks)
guessing()
def guessing():
global fullWordList
global chances
print("guess a letter")
global guess
guess=input('>')
guessloop()
def guessloop():
global chances, guess, blanks, blankList, fullWordList
# three possibilities: correct guess, repeated guess, incorrect guess
# 1. correct guess
if guess in fullWordList:
#generates list of indices of ALL matches
# stackoverflow.com/questions/9542738/python-find-in-list
letterIndex = [i for i, x in enumerate(fullWordList) if x == guess]
print letterIndex
for idx in letterIndex:
# change elements in blank list if there's a match
blanks[idx] = (guess +' ')
# update fullWordList to replace correct guesses with '-'
fullWordList[idx] = ('-')
# display the updated blank list as a string
print ''.join(blanks)
# replace all occurrences of guess from fullWordList with '-'
print ("Guess is correct!")
# add guess to wrongnums to record it as 'already guessed'
wrongnums.append(guess)
# check if it's a winning guess
if fullWordList == ['-']*len(fullWord):
print()
# display updated list as string
print("Word: ", ''.join(blanks))
win()
# 2. repeated guess
elif guess in wrongnums:
# don't add to wrongnums if already guessed (letter is already in there)
if guess in wrongnums:
print("You've guessed that!")
print ''.join(blanks)
# 3. incorrect guess
else:
chances -=1
wrongnums.append(guess)
print (wrongnums)
print ("Guess is wrong! ", chances, " more failed attempts allowed.")
print ''.join(blanks)
if chances == 0:
gameOver()
guessing()
def check():
print()
print("Word: ",newBlanks)
guessing2()
def win():
print("You guessed it well done")
print("Would you like to play again?")
PG = input(">")
if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
print ("Game will restart in 5 seconds")
sleep(5)
hangman()
else:
print("Going to main menu in 5 seconds")
sleep(5)
start()
def gameOver():
print("You ran out of guesses, sorry, you lose")
print("Would you like to play again?")
PG = input(">")
if PG == "yes" or PG == "Y" or PG == "Yes" or PG == "yea" or PG == "Yea":
print ("Game will restart in 5 seconds")
sleep(5)
hangman()
else:
print("Going to main menu in 5 seconds")
sleep(5)
start()
hangman()