将用户输入与字符串匹配?

时间:2017-11-29 20:42:35

标签: python

我正在尝试打印一些代码,这些代码将User的输入与之前的String Input相匹配。然后显示角色匹配的位置(如游戏Hangman)。

' - '表示第一个字符串的外观,每个' - '一旦输入正确的猜测,将被删除。现在我尝试使用' =='方法,但似乎没有做到这一点,有人有任何建议吗?

word_guessed = input("Enter a Word to be guessed: ")
type(len(word_guessed))
print(len(word_guessed)* '-')

guess_letter = input("Enter a letter to be guessed: ")
if guess_letter == word_guessed:
    print("Correct Guess, Guess again: ")
    print("The Letter is located:" == len(word_guessed))
else:
    guess_letter = word_guessed
    print("Incorrect Character, again.")

2 个答案:

答案 0 :(得分:0)

我有一些代码,我提取了最相关的部分。阅读它,我认为它回答了你的问题:

alpha = list("abcdefghijklmnopqrstuvwxyz") # valid inputs
secretword = "animal" # the secret word
found = [] # list with letters that are found

while True:
    print(' '.join(i if i in found else '_' for i in secretword))

    # Only accept valid inputs
    while True:
        inp = input("Guess a letter [a-z]: ")
        if inp in alpha:
            break
        else:
            print("Not valid!")

    if inp in secretword:
        print("Nice")
        found.append(inp) # add letter to found list
        alpha.remove(inp) # remove letter from alpha (valid inputs)

    # If len of unique letters (set) in secretword equals len found
    # Break the game
    if len(set(secretword)) == len(found):
        print("You won")
        break

答案 1 :(得分:0)

第一部分,检查字母或符号是否在字符串中:

if letter in string: 
    # do something

只要"字母"这将返回true它的确切形式存在于字符串中。不同的大小写,输入的空格或其他差异将注册为false,因此您可能需要在检查之前清理用户的输入。

  

a ='工厂'

     

     

' F'是假的

     

' F'在一个

     

是的

     

' F'在一个

     

False(注意F之后的空格)

其次,要找到字母的索引,有几种方法:

mystring.index('letter')
# Returns the index of 'letter', but only the first one if there are more than one.
# Raises ValueError if not found.

或者我们可以使用列表推导来遍历字符串并返回所有索引的列表,其中包含' letter'是:(https://stackoverflow.com/a/32794963/8812091

indices = [index for index, char in enumerate(mystring) 
           if char == 'letter']
# Go over the string letter by letter and add the letter position 
# if it matches our guessed letter.

第三,要使用找到的字母更新字符串,我们将重复“' ----'字符串并将猜测的字母放在正确的位置。见下文。

将它放入你的脚本:

word_guessed = input("Enter a Word to be guessed: ")
type(len(word_guessed))
print(len(word_guessed)* '-')



current_guess = str('-' * len(word_guessed))
# The current state of the guessed word fixed as a variable for updating
# and printing.

guess_letter = 'F'
# Fixed input letter for testing with the word "Factory".

if guess_letter in word_guessed: 
    letter_indices = [index for index, char in enumerate(word_guessed) if char == guess_letter]
    # Or if you want to show only one (the first) of the guessed letters:
    # letter_indices = word_guessed.index(guess_letter) 
    print("Correct Guess, Guess again: ")
    print("The Letter is located:", letter_indices)

# Now update the current guess to include the new letters.
current_guess = ''.join(guess_letter if index in letter_indices 
                        else char for index, char in enumerate(current_guess))
# We can't change strings, so we build a new one by iterating over the current guess.
# Put in the guess letter for the index positons where it should be, otherwise keep 
# the string as is. ''.join() is an efficient way of combining lots of strings.

print(current_guess)
  

输入要猜测的Word:Factory

     

" -------" (添加"以避免SO自动格式化)

     

正确猜猜,再猜一遍:

     

信件位于:[0]

     

˚F------