【问题标题】:How to compare user inputted string to a character(single letter) of a word in a list?如何将用户输入的字符串与列表中单词的字符(单个字母)进行比较?
【发布时间】:2019-05-25 20:50:56
【问题描述】:

我正在尝试使用 python 创建一个刽子手游戏。我知道还有很多事情要完成,但我正在尝试找出游戏的主要组成部分,即如何将用户输入的字符串(一个字母)与三个随机选择的单词之一中的字母进行比较。

import random

print("Welcome to hangman,guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
guess = input(str("Enter your guess:"))
guess_left = 10
guess_subtract = 1
if guess == "":
    guess_left = guess_left - guess_subtract
    print("you have" + guess_left + "guesses left")

【问题讨论】:

  • 请不要链接到代码,您的问题应该是独立的。也就是说,刽子手问题每天都会出现。您肯定从搜索中找到了一些可行的基本解决方案吗?
  • 代码必须在您的帖子中,而不是在外部链接、图像或 cmets 中。如果这些外部网站在 5 年后出现故障怎么办?
  • 用户是否希望输入一个单词或一个字母?
  • 他们应该输入一个字母

标签: python string list


【解决方案1】:

您可以将字符串视为数组/列表。 所以如果你使用循环:

x = ""#let's assume this is the users input
for i in range(len(y)): #y is the string your checking against
    if x == y[i]:

从这里我的想法是,如果它们相等,它将将该字母添加到字符串中

假设字符串是“今天很有趣”,那么会有一个像这样的数组,[""."" 等等,但你必须找到一种方法来考虑空格和撇号。所以如果字母是 == 到字符串的那一部分

#this is a continuation of the code, so under if
thearray[theposition] == the letter

但是看你的代码,你的游戏与普通的不同,所以,也考虑使用一段时间看看

gamewon = False
number of trials = 10

while gamewon == False and number of trials > 0:
    #the whole checking process 
    if x == guess:
       gamewon == True
       print ........# this is if your doing word for word else use 
       the code above
     else:
         number  of trials = number of trials - 1

我想就是这样我知道它有点到处但是你可以随时寻求帮助,我会解释另外我指的是你的代码我认为你可以修复的东西

【讨论】:

    【解决方案2】:

    首先,您应该使用guess = guess.lower() 同时接受aA。您必须在字符串中搜索猜测的字符并找到位置。

    guess = guess.lower()
    
    # Find the position(s) of the character guessed
    pos = [idx for idx,ch in enumerate(correct_word) if ch == guess]
    
    if pos:
        print('Letter Found')
    
    guess_left -= 1
    

    理解for循环的另一种方式是:

    pos = []
    for idx,ch in enumerate(correct_word):
        if ch == guess:
            pos.append(idx)
    

    【讨论】:

    • 我还是 python 新手,我很难弄清楚这一行告诉程序要做什么:pos = [idx for idx,ch in enumerate(correct_word) if ch == guess ]
    • 这是另一种编写 for 循环的方法。它说,对于枚举列表中的 idx,ch,第一个字符将在变量 ch 中,索引为 0。然后下一个 ch 将是下一个索引为 1 的字符。然后 if 是仅检查与猜测的字母相等的ch。如果这是真的,则将 idx 放入名为pos 的列表中。这是 python 将 for 循环和 if 语句组合成一行的奇特方式。
    【解决方案3】:

    我认为你需要一个骨架,然后花一些时间来改进游戏。

    import random
    
    print "Welcome to hangman, guess the five letter word"
    
    words = ["china", "ducks", "glass"]
    correct_word = (random.choice(words))
    
    trials = 10
    
    for trial in range(trials):
        guess = str(raw_input("Enter character: "))
    
        if (len(guess) > 1):
            print "You are not allowed to enter more than one character at time"
            continue
    
        if guess in correct_word:
            print "Well done! '" + guess + "' is in the list!"
        else:
            print "Sorry " + guess + " does not included..."
    

    您的下一步可能是打印出 c_i__ 之类的内容以及剩余的试验次数。玩得开心:)

    完成实现后,请花一些时间并使用函数重新实现它。

    【讨论】:

    • 感谢您的建议,我一定会添加这个!
    • 抱歉没有15声望。
    • 对不起,如果你能帮助我,我还有一个问题。当单词中的所有字母都猜对了时,我怎么能告诉程序停止接受猜测?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    相关资源
    最近更新 更多