【问题标题】:Check for repetition in input (Python)检查输入中的重复(Python)
【发布时间】:2017-06-14 10:15:19
【问题描述】:

如何检查刽子手游戏的重复输入(字母)?

例子:

单词是苹果

输入是猜一个字母:a

输出做得很好!

然后猜下一个单词

输入是猜一个字母:a

输出应该是你已经猜到了那个字母。

我的代码:

def checkValidGuess():
word = getHiddenWord()
lives = 10
num = ["1","2","3","4","5","6","7","8","9",]
#guessed = ''
while lives != 0 and word:
    print("\nYou have", lives,"guesses left")
    letter = input("Guess a letter or enter '0' to guess the word: ")
    if letter.lower() in word:
        print("Well done!", letter, "is in my word!")
        lives -= 1
    elif len(letter)>1:
        print("You can only guess one letter at a time!")
        print("Try again!")
    elif letter in num:
        print("You can only input letter a - z!")
        print("Try again!")
    #elif letter in guessed:
        #print("repeat")
    elif letter == "0":
        wword = input("What is the word?").lower()
        if wword == word:
            print("Well done! You got the word correct!")
            break
        else:
            print("Uh oh! That is not the word!")
            lives -= 1
    #elif letter == "":
        #print("Uh oh! the letter you entered is not in my word.")
        #print("Try again!")
    else:
        print("Uh oh! the letter you entered is not in my word.")
        print("Try again!")
        lives -= 1

谢谢。

【问题讨论】:

  • 到目前为止你得到了什么?
  • 欢迎来到 SO:向我们展示您目前拥有的东西。
  • 添加了代码@MateenUlhaq
  • 代码添加@ShawnMehan

标签: python python-3.x input


【解决方案1】:

您可以将输入存储在一个列表中,我们称之为temp。

然后当用户输入新字母时,您可以检查输入是否存在于列表中。

guess = input()
if guess in temp:
    print "You've already guessed {}".format(guess)
else:
    #do whatever you want

【讨论】:

    【解决方案2】:

    这是一个简单的方法。首先初始化一个列表:

    guesses = []
    

    然后在你的循环中:

    letter = input("Guess a letter or enter '0' to guess the word: ")
    
    if letter in guesses:
        print("Already guessed!")
        continue
    
    guesses.append(letter)
    

    【讨论】:

    • 弹出这个错误:AttributeError: 'list' object has no attribute 'add'
    • 对不起,我的意思是.append
    • 它只适用于单词中没有的字母。例如,单词是苹果,如果您输入“a”,第二次输入另一个“a”,它仍然会检测为 Well Done。但是,如果您输入“t”,而第二次输入另一个“t”,则表示您已经猜到了。它很接近 :) 我想我在某个地方搞砸了正确的单词?
    • 把它放在最上面,在所有其他ifs 之前。请注意订购ifs 的方式。
    • 天啊!!谢谢老兄,它成功了:D 非常感谢!!!感谢帮助:D
    【解决方案3】:

    因此,您可能希望颠倒程序中检查的顺序,以便首先处理 try again 内容。在该更改之后,添加另一个条件来确定字母是否与已经猜到的字母匹配。结果如下:

    already_guessed = set()  # I made this a set to only keep unique values
    
    while lives > 0 and word: # I changed this to test > 0
        print(f"\nYou have {lives} guesses left") # I also added an f-string for print formatting
        letter = input("Guess a letter or enter '0' to guess the word: ")
        if len(letter) > 1:
            print("You can only guess one letter at a time!")
            print("Try again!")
            continue # If you reach this point, start the loop again!
        elif letter in already_guessed:
            print("You already guessed that!")
            print("Try again")
            continue
        elif letter in num:
            print("You can only input letter a - z!")
            print("Try again!")
            continue
        elif letter.lower() in word:
            print("Well done!", letter, "is in my word!")
            lives -= 1
        else:  
            already_guessed.update(letter)
            # It wasn't a bad character, etc. and it wasn't in the word\
            # so add the good character not in the word to your already guessed 
            # and go again!
    

    您需要添加其他条件分支,但这应该可以帮助您。祝你好运。

    【讨论】:

    • 它只适用于单词中没有的字母。例如,单词是苹果,如果您输入“a”,而第二次输入另一个“a”,它仍然会检测为 Well Done。但是,如果您输入“t”,而第二次输入另一个“t”,则表示您已经猜到了。它很接近:) @ShawnMehan
    • @Ned,我没有完成完整的while 循环。我只解决了其中的一部分……我只是想告诉你如何处理已经猜到的字母。你需要我为你写完整的程序吗?我以为你想要一个提示,因为很明显你是作为一个项目来做的,而不是完成的工作......
    • 没关系,我解决了,但现在我坚持将它们分成不同的功能,你想帮我吗?
    猜你喜欢
    • 2017-01-31
    • 2013-05-24
    • 1970-01-01
    • 2014-11-05
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 2011-11-19
    相关资源
    最近更新 更多