如何将用户输入的字符串与列表中单词的字符(单个字母)进行比较



我正在尝试用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")
我认为

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

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__以及剩余的试验数量。玩得开心:)

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

我昨天完成了这个项目; (对于阅读此问题的其他人)如果需要,请获得灵感。

import random
stages = ['''
  +---+
  |   |
  O   |
 /|  |
 /   |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|  |
 /    |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|  |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
 /|   |
      |
      |
=========''', '''
  +---+
  |   |
  O   |
  |   |
      |
      |
=========
''', '''
  +---+
  |   |
  O   |
      |
      |
      |
=========
''', '''
  +---+
  |   |
      |
      |
      |
      |
=========
''']
#used to choose random word
random_lst=['addition','adequate','adjacent','adjusted','advanced','advisory','advocate','affected','aircraft','alliance','although','aluminum','analysis','announce','anything','anywhere','apparent','appendix','approach','approval','argument','artistic','assembly']
chosen_word = random.choice(random_lst)

#used to create display list + guess var
print("Welcome to hangman! ")
print("--------------------")
print("you have 6 lives." )
print("   ")
print (stages[-1])
display = []
for letters in chosen_word:
  display += "_"
print(display)
print("  ")
#used to check if guess is equal to letter, and replace pos in display if yes. 

lives = 6
game_over = False
#use the var guesses to store guesses and print at every guess to let player know what letters they tried already.
guesses = ""

while not game_over:
  guess = input("Guess a letter. ")
  print("  ")
  guesses += (guess + " , ")
  for pos in range(len(chosen_word)):
    letter = chosen_word[pos
    if letter == guess:]
      display[pos] = letter
      print("√")
      print("- - -")
      print("   ")
  if display.count("_") == 0:
    print("Game over. ")
    game_over = True
  elif guess not in chosen_word:
    lives -= 1
    print(f"Wrong letter. {lives} lives left. ") 
    print("- - - - - - - - - - - - - - - - - - -")
    print("   ")
  if lives == 0:
    print (stages[0])
  elif lives == 1:
    print (stages[1])
  elif lives == 2:
    print (stages[2])
  elif lives == 3:
    print(stages[3])
  elif lives == 4:
    print(stages[4])
  elif lives == 5:
    print(stages[5])
  elif lives == 6:
    print(stages[6])
  elif lives == 0:
    game_over = True
    print("Game over. ")
  print(f"letters chosen: {guesses}") 
  print("   ")
  print(display)
  print("   ")
  if lives == 0 or display.count("_") == 0:  
    break
  
if lives == 0:
  print("Game over. You lose.")
elif lives > 0:
  print("Congrats, you win! ")
 

你可以做的是把字符串当作数组/列表。因此,如果您使用循环:

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

我想就是这样,我知道它有点到处都是,但您可以随时寻求帮助,我会解释加上我指的是我认为您可以修复的代码

首先,您应该使用 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)

最新更新