使用while和if-else循环的简单子手游戏无法正确迭代 [英] Simple hangman game using while and if-else loop does not iterate correctly

查看:45
本文介绍了使用while和if-else循环的简单子手游戏无法正确迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用简单的while循环和if else语句设计一款Hangman游戏.

I am trying to design a hungman game using simple while loop and if else statement.

游戏规则:

1.从单词列表中选择随机单词

1.Random word is selected from a list of words

2.选择单词时会通知用户,并要求用户提供他/她的第一个猜测

2.User is informed when the word is selected and asked to provide his/ her first guess

3.如果用户的猜测正确,则字母为控制台,并告知用户还剩多少字母

3.If the user's guess is correct, the letter is console and inform the user how many letters left

  1. 用户只有5条生命可以玩游戏.

  1. The user will get only 5 lives to play the game.

 1.import random
 2.import string
 3.def hungman():
 4.words = ["dog", "monkey", "key", "money", "honey"]
 5.used_letters = []
 6.word = words[random.randrange(len(words))]
 7.lives=5
 8.while len(word) > 0 and lives > 0:
     9.guess = input("Please guess the letter: ")
     10.if 'guess' in word:
        11.print(guess, " is a correct guess")
        12.used_letters.appened(guess)
        13.if used_letters.len == word.len:
            14.print("Congratulation, You have guessed the correct letter: ",word)      
        15.else:
            16.remaining_letters =  word.len - used_letters.len
            17.print("You have", remaining_letters, "left")


      18.else:
      19.lives = lives - 1
      20.if lives == 0:
          21.print("Sorry! you don't have more lives to continue the game")
          22.print("Correct word is: ", word)
          23.break
      24.else:
          25.print("You have", lives , " left")

 26.hungman()

程序应要求用户输入,并将其存储在 guess 变量中.如果用户给出的字母是单词字符串的字母之一,则代码会提示给定的输入正确,并将字母追加到used_letters列表中.否则,它会显示用户输入错误时剩余字母的长度.此外,如果用户未能正确猜出字母,他或她也将丧命1次.

The program should ask for the user input which will store in guess variable. If the letter given by the user is one of the letters of word string then the code prompts that the given input is correct and append the letter in the used_letters list. Else it shows the length of the remaining letters for wrong user input. Additionally, if the user fails to guess the letter correctly he or she will lose 1 life as well.

但是,根据我的代码,在第8行的while循环之后,控制转到第6行.18,尽管我提供了正确的字母作为用户输入.第10到17行被完全丢弃.我找不到这种性质的原因.

However, as per my code after the while loop in line number 8 the control goes to line no. 18 although I provided correct letter as user input. Line 10 to 17 totally discarded. I could not find the reason of this nature.

在这方面请帮助我.

谢谢

推荐答案

您的代码中有几个问题.您提到的一个是由于第10行中的引号引起的.应该是

You have a couple of issues in your code. The one you mentioned is because of quotation marks in line 10. Should be

if guess in word:

在第12行中您有错字

used_letters.append(guess)

要获取列表的长度,您应该使用函数 len(),例如

to get the length of a list you should use function len(), e.g.

if len(used_letters) == len(word)

最后,在正确答案的情况下退出循环会遇到问题.您应该使用

And finally you have an issue with exiting the loop in case of the correct answer. You should use

while len(word) > len(used_letters) and lives > 0:

这篇关于使用while和if-else循环的简单子手游戏无法正确迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆