Python ValueError:“f”不在列表中,但它显然在列表中
假设用户输入了字母“f”。然后弹出下面的错误Python ValueError:“f”不在列表中,但它显然在列表中,python,parsing,Python,Parsing,假设用户输入了字母“f”。然后弹出下面的错误 wrdsplit = list('ferret') guess = input('> ') # user inputs `f` print(wrdsplit.index(guess)) 但这会导致ValueError:“f”不在列表中。根据您以前的代码,我在这里看到的唯一错误是: if guess in wrdsplit: for count in range(len(wrdsplit)): x = wrdsplit
wrdsplit = list('ferret')
guess = input('> ')
# user inputs `f`
print(wrdsplit.index(guess))
但这会导致ValueError:“f”不在列表中。根据您以前的代码,我在这里看到的唯一错误是:
if guess in wrdsplit:
for count in range(len(wrdsplit)):
x = wrdsplit.index(guess)
wrdsplit[x] = '&'
correct[x] = guess
g = g + 1
wrdsplit[x] = '&' # x is out of scope
if "".join(correct) == word:
print("Well done... You guessed the word in", g ,"guesses and you got", w , "wrong")
print("YOU LIVED")
break
现在,若您要继续在此处重试查找f,它将不起作用,因为您已将“f”更改为“&”,所以您将得到一个不在列表中的f的错误。你刚刚陷入了一个讨厌的小虫子循环
<>你应该考虑使用字典来代替这个。 试试这段代码,看看它是否有效,是否做了你想做的事
# Get this from a real place and not hardcoded
wrdsplit = list("ferret")
correct = ["-"]*len(wrdsplit) # We fill a string with "-" chars
# Ask the user for a letter
guess = input('> ')
if guess in wrdsplit:
for i, char in enumerate(wrdsplit):
if char == guess:
correct[i-1] = guess
# Calculate the number of guesses
if "-" in correct:
g = len(set(correct)) - 1
else:
g = len(set(correct))
# Print the right word
print("".join(correct))
集合生成一个不带重复项的数组,以计算他进行了多少次猜测,如果找到了-,则会减去一个,因为该字符不是猜测。OMG。。。。。。。非常感谢你们的帮助。我终于修好了。在这里玩我的刽子手游戏玩得开心:
import random
import time
import collections
def pics():
if count == 0:
print (" _________ ")
print ("| | ")
print ("| 0 ")
print ("| /|\ ")
print ("| / \ ")
print ("| ")
print ("| ")
elif count == 1:
print (" _________ ")
print ("| | ")
print ("| 0 ")
print ("| /|\ ")
print ("| / ")
print ("| ")
print ("| ")
elif count == 2:
print (" _________ ")
print ("| | ")
print ("| 0 ")
print ("| /|\ ")
print ("| ")
print ("| ")
print ("| ")
elif count == 3:
print (" _________ ")
print ("| | ")
print ("| 0 ")
print ("| /| ")
print ("| ")
print ("| ")
print ("| ")
elif count == 4:
print (" _________ ")
print ("| | ")
print ("| 0 ")
print ("| | ")
print ("| ")
print ("| ")
print ("| ")
elif count == 5:
print (" _________ ")
print ("| | ")
print ("| GAME OVER ")
print ("| YOU LOSE ")
print ("| ")
print ("| ")
print ("| (**)--- ")
print("Welcome to HANGMAN \nHave Fun =)")
print("You can only get 5 wrong. You will lose your :\n1. Right Leg\n2.Left Leg\n3.Right Arm\n4.Left Arm\n5.YOUR HEAD... YOUR DEAD")
print("")
time.sleep(2)
words = "ferret".split()
word = random.choice(words)
w = 0
g = 0
count = 0
correct = []
wrong = []
for i in range(len(word)):
correct.append('#')
wrdsplit = [char for char in "ferret"]
while count < 5 :
pics()
print("")
print("CORRECT LETTERS : ",''.join(correct))
print("WRONG LETTERS : ",wrong)
print("")
print("Please input a letter")
guess = input("> ")
loop = wrdsplit.count(guess)
if guess in wrdsplit:
for count in range(loop):
x = wrdsplit.index(guess)
correct[x] = guess
wrdsplit[x] = '&'
g = g + 1
if "".join(correct) == word:
print("Well done... You guessed the word in", g ,"guesses and you got", w , "wrong")
print("YOU LIVED")
break
elif guess not in wrdsplit:
wrong.append(guess)
w = w + 1
g = g + 1
count = count +1
pics()
print("The correct word was : ",word)
你能试着做一个像中这样的小例子吗?wrdsplit=listword应该是wrdsplit=listword[0]。基本上,ferret.split返回['ferret']。什么是ferret,什么是guess请描述一下。这太简单了,你至少需要提供guess和wrdsplit是什么的示例。请注意。但是@AshwiniChaudhary中的完整内容。。。我只有雪貂。分开,因为以前,我没有雪貂,而是有一长串单词。我将它们全部删除,以便集中精力修复重复的错误。执行wrdsplit=listword[0]时,它仅将“f”与单词分开。它并没有把所有的字符分开,但我认为这是不正确的。因为我已经解释了“f”,我只想解释一次,所以在我将它更改为“&”之后,for循环应该在不做任何操作的情况下运行,然后继续。对!但在原始代码中,您试图在for循环之外再次设置它。我很高兴你让它工作了!我在那里做了一些更正,从使用Pyton PEP风格的单引号打印到计数器重新思考,因为你有一些是无用的。检查每一个差异,如果你不明白,请询问。