首页 > 解决方案 > 用 Python 编写程序以显示在刽子手游戏中猜到的字母不起作用

问题描述

我必须用 Python 编写一个关于玩游戏刽子手的代码。目前我的代码无法用猜测的字母正确替换“_”,它只替换一个并且不显示所有猜测的字母。

我的代码如下:

import random

word = random.choice(["bread", "clouds", "plane"])

guess = 0
correct_guess = 0
play_again = True

print(word)
name = input("Hello, what's your name? ")
print('Hello', name)
status = input('Do you wish to play hangman? Yes/No ')
hyphens = print('_ ' * (len(word)))

if status == 'No' 'no' 'N' 'n':
  exit()

while play_again is True:

  letter = input('Guess a letter: ')

  if letter in word:
    guess = guess + 1
    correct_guess = correct_guess + 1
    print('You guessed a letter correctly!')
    position = word.find(letter)
    print("The letter is in position", position + 1, "out of", len(word), "in the word. Guess Count:", guess)
    if position == 0:
      print(letter, "_ _ _ _")
    if position == 1:
      print("_", letter, "_ _ _ ")
    if position == 2:
      print("_ _", letter, "_ _ ")
    if position == 3:
      print("_ _ _", letter, "_ ")
    if position == 4:
      print("_ _ _ _", letter)
  else:
    guess = guess + 1
    print("That letter isn't in the word. Guess Count:", guess)
  if guess == 10:
    print('Bad luck. You lost.')
    play = input('Do you want to play again? ')
    if play == 'No' 'no' 'N' 'n':
      exit()
  if correct_guess == 5:
    print('Congratulations! You have guessed the word! The word was', word + '.')
    play = input('Do you want to play again? ')
    if play == 'No' 'no' 'N' 'n':
      exit()

非常感激您的帮忙。另外,我对编程知之甚少,而且我对它还很陌生。

詹姆士

标签: python

解决方案


你不能写这样的表达式:

if status == 'No' 'no' 'N' 'n':

正确的方法是:

if play == 'No' or play == 'no' or play == 'N' or play == 'n':

或者:

if play in ['No' ,'no', 'N' ,'n']

一种解决方案:

import random

word = random.choice(["bread", "clouds", "plane"])


print(word)
name = input("Hello, what's your name? ")
print('Hello', name)
status = input('Do you wish to play hangman? Yes/No ')


def play():
    if status == 'No' or status == 'no' or status == 'N' or status == 'n':
        print ("Goodbye")
        return # exit 
    else:
        print('_ ' * (len(word))) # _ _ _ _ _ 

    guess = 0
    correct_guess = 0
    play_again = True

    pos_list = ['_' for x in range(len(word))] # define list where you will put your guessed letters ['_', '_', '_', '_', '_']
    while play_again is True:
      letter = input('Guess a letter: ')

      if letter in word:
        guess = guess + 1
        correct_guess = correct_guess + 1
        print('You guessed a letter correctly!')
        position = word.find(letter)
        print("The letter is in position", position + 1, "out of", len(word), "in the word. Guess Count:", guess)
        pos_list[position] = letter # save letter at position # ['_', '_', '_', 'a', '_']
        print (' '.join(pos_list)) # _ _ _ a _

      else:
        guess = guess + 1
        print("That letter isn't in the word. Guess Count:", guess)
      if guess == 10:
        print('Bad luck. You lost.')
        play = input('Do you want to play again? ')
        if play == 'No' or play == 'no' or play == 'N' or play == 'n':
            print("Goodbye")
            return
        else:
             print('_ ' * (len(word)))
      if correct_guess == len(word):
        print('Congratulations! You have guessed the word! The word was', word + '.')
        play = input('Do you want to play again? ')
        if play == 'No' or play == 'no' or play == 'N' or play == 'n':
            print("Goodbye")
            return
        else:
            print('_ ' * (len(word)))

play()

推荐阅读