【问题标题】:ValueError: 'f' is not in list but it clearly isValueError:“f”不在列表中,但显然是
【发布时间】:2017-07-11 05:23:12
【问题描述】:

假设用户输入(编辑)了字母'f'。然后弹出下面的错误。

wrdsplit = list('ferret')
guess = input('> ')
#  user inputs `f`
print(wrdsplit.index(guess))

但这会导致ValueError: 'f' is not in list

【问题讨论】:

  • 你能试着做一个最小的例子吗(比如minimal reproducible example)?
  • wrdsplit = list(word) 应该是 wrdsplit = list(word[0])。基本上"ferret".split() 返回['ferret']
  • 什么是雪貂,什么是猜测请描述
  • 现在太少了,您至少需要提供(示例)guesswrdsplit 是什么。 (注意minimal reproducible example中的完整内容)。
  • 但是@AshwiniChaudhary...我只有“ferret”.split() 因为以前我有一个很长的单词列表,而不是“ferret”。我将它们全部删除,以便我可以专注于修复重复的错误。当您执行 wrdsplit = list(word[0]) 时,它只会将“f”与单词分开。它不会分隔所有字符

标签: python parsing


【解决方案1】:

我在这里看到的唯一错误(基于您之前的代码)是:

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 不在列表中的错误。你刚刚陷入了一个令人讨厌的小错误循环。

您应该考虑为此使用字典。

【讨论】:

  • 谢谢,但我认为这是不正确的。因为我已经考虑了'f'并且我只想考虑一次所以在我将它更改为'&'之后,for循环应该不做任何事情运行然后继续。
  • 对!但是在您的原始代码中,您试图在 for 循环之外再次设置它。我很高兴你让它工作了!
【解决方案2】:

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)

【讨论】:

  • 我在那里做了一些更正,从根据 Pyhton PEP 样式使用单引号进行打印,到反击重新思考,因为你有一些无用的。检查每一个差异,如果你不明白,请询问。
【解决方案3】:

试试这段代码,看看它是否有效,是否符合你的要求

# 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))

该集合生成一个不重复的数组来计算他做了多少次猜测,如果找到“-”,则减去一个,因为该字符不是猜测。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2021-04-11
    • 2020-01-23
    • 2017-09-17
    • 2012-06-04
    • 2017-12-29
    • 2022-12-17
    相关资源
    最近更新 更多