簡體   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)

當我輸入 hello 作為我的單詞時,它會正確輸入,當我輸入第一個字母 hello 時,它會將第一個下划線切換為“h”。 但是當我輸入“e”時,它會放錯位置,當我輸入“l”時,它會給我一個錯誤。 有人可以告訴我問題是什么嗎?

您描述的問題在代碼的這一部分中:

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)

當你在 'hello' 中輸入 'h' 時, lettersinword變為4 (因為在 'hello' 中每個字母都加1 ),所以當你再輸入 'e' 時6當語句lettersinword listspaces[lettersinword] = letterguessed已運行。 您需要在每個 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)

還有另一個需要修復的錯誤: spaces"h _ _ _ _ " ,所以listspaces變成['h', ' ', '_', ' ', '_', ' ', '_', ' ', '_', ' '] 輸入 'e' 時, lettersinword現在是1 ,因此listspaces變為['h', 'e', '_', ' ', '_', ' ', '_', ' ', '_', ' '] ,空格變成"he_ _ _ _ " ,而不是"he _ _ _ " 一個快速的解決方法是將增量更改為2

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

但是,我建議將列表空間listspaces為您的字母/下划線列表,沒有空格分隔它們,並在每次要打印時從該列表創建字符串。 由於listspaces空間在字母之間不再有多余的空格,因此計數器每次迭代都可以增加1而不是2 此外,每次迭代列表並每次迭代遞增計數器時,最好使用內置的enumerate 這是具有這些更改的版本:

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:在 Python 中,我們通常使用“蛇形大小寫”來表示變量(和函數)名稱,例如letters_guessed而不是lettersguessed 這正是社區習慣閱讀的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM