简体   繁体   English

我对这个刽子手程序有点麻烦。 有人可以帮我弄清楚吗?

[英]I am having a bit of trouble with this hangman program. Can someone help me figure it out?

wanttodo = input('Type 1 if you want rules and 2 if you want to start and 3 if you want to end the program')

while wanttodo!='3':
  if wanttodo == '1':
    print('Player 1 picks a word (cannot include spaces or dashes) and player two goes guessing letters until the word is complete. You have six chances to get the letters wrong. Player one wins if player 2 doesnt guess it and player 2 wins if he does guess it.')
  elif wanttodo=='2': 
    word = str(input('Player 1: What is your word?'))
    word = word.lower()
    wordlong = len(word)
    rightanswer = False
    lives = 6
    lettersinword = -1

    guessed_letters = list()
    nooflettersguessed = len(guessed_letters)
    for x in range (100):
      print('')
    spaces = ('_ '*wordlong)
    print(spaces)
    while lives>0 and rightanswer == False:
      letterguessed = input('Pick a letter. Must not be picked yet.')
      letterguessed = letterguessed.lower()
      if len(letterguessed)!=1 or not letterguessed.isalpha():
        print('Your guess is not valid. Try again.')
      elif letterguessed in guessed_letters:
        print('Your pick has already been chosen. You have not lost a live. Guess again') 
      else:
        print('You picked a valid and new letter.')
        if letterguessed not in word:
          print(letterguessed + ' is not in the word.')
          lives-=1
        elif letterguessed in word:
          print('Lovely! ' + letterguessed + ' is in the word.')
          for letter in word:
            lettersinword+=1
            if letter == letterguessed:
              listspaces = list(spaces)
              listspaces[lettersinword] = letterguessed
              spaces = ''.join(listspaces)
        print(spaces)

        guessed_letters.append(letterguessed)
        print(guessed_letters)

When I type in hello as my word, it takes it in properly, when I make my first letter hello, it switches the first underscore to the "h".当我输入 hello 作为我的单词时,它会正确输入,当我输入第一个字母 hello 时,它会将第一个下划线切换为“h”。 But when I type in an 'e', it puts it in the wrong spot, and when I type in 'l', it gives me an error.但是当我输入“e”时,它会放错位置,当我输入“l”时,它会给我一个错误。 Can someone tell me what the problem is?有人可以告诉我问题是什么吗?

The problems you describe are in this part of your code:您描述的问题在代码的这一部分中:

lettersinword = -1
while lives>0 and rightanswer == False:
    ...
    for letter in word:
        lettersinword+=1
        if letter == letterguessed:
            listspaces = list(spaces)
            listspaces[lettersinword] = letterguessed
            spaces = ''.join(listspaces)

When you enter the 'h' in 'hello', lettersinword is changed to 4 (because it is incremented by 1 for every letter in 'hello'), so when you then enter 'e', lettersinword is 6 when the statement listspaces[lettersinword] = letterguessed is run.当你在 'hello' 中输入 'h' 时, lettersinword变为4 (因为在 'hello' 中每个字母都加1 ),所以当你再输入 'e' 时6当语句lettersinword listspaces[lettersinword] = letterguessed已运行。 You need to reset lettersinword before each for-loop:您需要在每个 for 循环之前重置lettersinword

while lives>0 and rightanswer == False:
    ...
    lettersinword = -1
    for letter in word:
        lettersinword+=1
        if letter == letterguessed:
            listspaces = list(spaces)
            listspaces[lettersinword] = letterguessed
            spaces = ''.join(listspaces)

There is another error that also needs fixing: spaces is "h _ _ _ _ " , so listspaces becomes ['h', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' '] .还有另一个需要修复的错误: spaces"h _ _ _ _ " ,所以listspaces变成['h', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' '] When entering 'e', lettersinword is now 1 , so listspaces becomes ['h', 'e', '_', ' ', '_', ' ', '_', ' ', '_', ' '] , and spaces becomes "he_ _ _ _ " , not "he _ _ _ " .输入 'e' 时, lettersinword现在是1 ,因此listspaces变为['h', 'e', '_', ' ', '_', ' ', '_', ' ', '_', ' '] ,空格变成"he_ _ _ _ " ,而不是"he _ _ _ " A quick fix would be to change the increment to 2 :一个快速的解决方法是将增量更改为2

lettersinword = -2
for letter in word:
    lettersinword+=2
    if letter == letterguessed:
        listspaces = list(spaces)
        listspaces[lettersinword] = letterguessed
        spaces = ''.join(listspaces)

However, I would recommend maintaining listspaces as your list of letters/underscores without spaces separating them, and creating the string from that list every time you want to print it.但是,我建议将列表空间listspaces为您的字母/下划线列表,没有空格分隔它们,并在每次要打印时从该列表创建字符串。 Since listspaces no longer has extra spaces in between letters, the counter can be incremented by 1 instead of 2 each iteration.由于listspaces空间在字母之间不再有多余的空格,因此计数器每次迭代都可以增加1而不是2 Also, every time you are iterating through a list and incrementing a counter every iteration, it is better to use the builtin enumerate .此外,每次迭代列表并每次迭代递增计数器时,最好使用内置的enumerate Here is a version with those changes:这是具有这些更改的版本:

wanttodo = input('Type 1 if you want rules and 2 if you want to start and 3 if you want to end the program')

while wanttodo != '3':
    if wanttodo == '1':
        print(
            'Player 1 picks a word (cannot include spaces or dashes) and player two goes guessing letters until the word is complete. You have six chances to get the letters wrong. Player one wins if player 2 doesnt guess it and player 2 wins if he does guess it.')
    elif wanttodo == '2':
        word = str(input('Player 1: What is your word?'))
        word = word.lower()
        wordlong = len(word)
        rightanswer = False
        lives = 6

        guessed_letters = list()
        nooflettersguessed = len(guessed_letters)
        for x in range(100):
            print('')
        listspaces = ['_'] * wordlong
        print(' '.join(listspaces))  # replacing: print(spaces)
        while lives > 0 and rightanswer == False:
            letterguessed = input('Pick a letter. Must not be picked yet.')
            letterguessed = letterguessed.lower()
            if len(letterguessed) != 1 or not letterguessed.isalpha():
                print('Your guess is not valid. Try again.')
            elif letterguessed in guessed_letters:
                print('Your pick has already been chosen. You have not lost a live. Guess again')
            else:
                print('You picked a valid and new letter.')
                if letterguessed not in word:
                    print(letterguessed + ' is not in the word.')
                    lives -= 1
                elif letterguessed in word:
                    print('Lovely! ' + letterguessed + ' is in the word.')
                    for lettersinword, letter in enumerate(word):
                        if letter == letterguessed:
                            listspaces[lettersinword] = letterguessed
                            spaces = ''.join(listspaces)
                print(' '.join(listspaces))  # replacing: print(spaces)

                guessed_letters.append(letterguessed)
                print(guessed_letters)

PS: in Python, we normally use "snake case" for variable (and function) names, eg letters_guessed rather than lettersguessed . PS:在 Python 中,我们通常使用“蛇形大小写”来表示变量(和函数)名称,例如letters_guessed而不是lettersguessed It's just what the community is used to reading.这正是社区习惯阅读的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以帮我做刽子手吗? - Can someone help me with hangman? 我在使用Python函数时遇到麻烦,有人可以帮我吗? - I am having troubles with Python functions, can someone help me? 我正在通过字典建立关系,但是有人可以帮我解决一个我不知道的输出吗? - I am making relationships from a dictionary, but can someone help me fix one output I can't figure out? 有人可以帮我弄清楚如何在我的 python 刽子手代码上更新我的破折号 - May someone help me figure out how to update my dashes on my python hangman code 我在pygame中创建移动平台时遇到了麻烦。 我遇到错误,有人可以帮我吗? - I am having trouble creating moving platforms in pygames. I am getting an error, can anyone help me out please? 有人能帮我算出算法的时间复杂度吗? - Can someone help me figure out the time complexity of my Algorithm? 有人可以帮我弄清楚如何打印此下拉列表中的选项吗? - Can someone help me figure out how to print the options in this dropdown? 有人可以帮我将 **for 循环** 替换为 **while 循环** 我正在努力弄清楚吗? - Can someone help me replace the **for loop** to **while loop** I'm struggling to figure it out? 我不知道如何解决这个问题,有人可以帮助我吗? Pirple作业python - I cannot figure out how to fix this problem, can someone help me ? Pirple homework python 我在 vsc 和 Windows 10 中的 Flask 中创建数据库时遇到问题。有人可以帮助解决这个问题吗? - I am having trouble creating the database in flask in vsc and in windows 10. Can someone help solve the problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM