2016-03-01 71 views
0

我在Python 3.5.1中創建一個hang子手遊戲,除了涉及猜測部分時,一切都進展順利。當信件被正確猜測時,我會打印「華友世界」,當它沒有被正確猜測時,我會畫出額外的身體部位。我的問題是,即使一個字母被正確地猜測它還將利用其它身體部位:Hang子手繪製無論正確的字母(Python,烏龜)

import drawHangman 
import turtle 
import random 

def main(): 
    window = turtle.Screen() 
    window.setup(400, 400, 200, 200) 
    HG = turtle.Turtle() 
    drawHangman.default(HG) 

    wrongGuess = 0 
    maxGuesses = 0 

    lines = open('wordlist.txt').read().splitlines() 
    myline =random.choice(lines) 
    print(myline) 

    name = input("Welcome to hangman! What is your name? \n") 
    print("Welcome", name + "! \n The rules are as follows: \n 1) No looking at answer list! \n 2) Do not have fun!! \n 3) No hacking my game!") 
    tos = input("Do you accept the rules provided above? Type Yes or No. \n") 

    if tos == "no": 
     print("Who cares! The rules suck anyways!") 
    elif tos == "yes": 
     print("HI") 
    else: 
     print("You don't listen very well. I guess proceed ugh") 

    while maxGuesses < 6: 
     guess = input("Please input your guess!") 
     maxGuesses += 1 
     for char in myline: 
      if char in guess: 
       print("Hurray") 
     else: 
      wrongGuess += 1 
      print("You have", 6 - maxGuesses, "guesses left!") 

     if wrongGuess == 1: 
      drawHangman.drawHead(HG) 
     elif wrongGuess == 2: 
      drawHangman.drawBody(HG) 
     elif wrongGuess == 3: 
      drawHangman.drawLeftArm(HG) 
     elif wrongGuess == 4: 
      drawHangman.drawRightArm(HG) 
     elif wrongGuess == 5: 
      drawHangman.drawLeftLeg(HG) 
     elif wrongGuess == 6: 
      drawHangman.drawRightLeg(HG) 

    else: 
     playAgain = input('Do you want to play again? (yes or no)') 
     if playAgain == "yes": 
      drawHangman.reset(HG) 
      main() 
     else: 
      print("Thanks for playing!") 
      return 

main() 

我知道它是與別的地方告訴它有一種錯誤的信,但我不能部分想辦法。我試着縮進一個空格,但這只是打印剩下的猜測量。任何幫助表示讚賞。

PS。 drawHangman.*例程來自我在我的src文件夾中包含正文部分的文件。

回答

0

您已經縮進else條款(及以下線)不正確 - 它在for循環,而不是if聲明範圍:

for char in myline: 
     if char in guess: 
      print("Hurray") 
     else: 
      wrongGuess += 1 
      print("You have", 6 - maxGuesses, "guesses left!") 

     if wrongGuess == 1: 
      drawHangman.drawHead(HG) 
     # etc. 

當你寫吧,else條文和所有的僅在整個for循環完成後執行圖紙。

+0

http://i.imgur.com/7uTKA1b.png HTTP://i.imgur .com/TGdiX3I.png 我嘗試過,但我得到奇怪的結果。 –

+0

我無法將第二個屏幕截圖中的輸出完全匹配到源代碼窗格中顯示的代碼部分。你的程序確實有更多問題。例如,爲什麼你要迭代用戶輸入中的所有字符?不僅應該看第一個字符嗎?每個輸入將至少有一個換行符,與您的測試字中的任何字母都不匹配。我還不清楚這條消息會如何打印三次。 –

0

除了在你的問題中提出的問題,你的代碼似乎還有其他問題:你的邏輯假設/硬編碼6個字母的單詞;你重複的遊戲邏輯實際上是遞歸的;完成後不要關閉單詞文件;你的是/否問題處理不穩健;你通過回憶main()重複一遍,只需要做一次;您不顯示當前正確/未知的字母狀態。

下面是你的代碼來解決這些問題,發揮劊子手的實際遊戲的返工:

from turtle import Turtle, Screen 
import random 
import drawHangman 

BODY_PARTS = [ \ 
    drawHangman.drawHead, drawHangman.drawBody, drawHangman.drawLeftArm, \ 
    drawHangman.drawRightArm, drawHangman.drawLeftLeg, drawHangman.drawRightLeg, \ 
    ] 

MAXIMUM_WRONG_GUESSES = len(BODY_PARTS) 

def hangman(HG): 
    drawHangman.default(HG) 

    with open('wordlist.txt') as file: 
     lines = file.read().splitlines() 

    word = random.choice(lines) 
    letters = set(word) 
    wrongGuesses = 0 

    while letters and wrongGuesses < MAXIMUM_WRONG_GUESSES: 

     for letter in word: 
      print("*" if letter in letters else letter, end="") 
     print(".") 

     letter = input("Please input your guess: ") 

     if letter in letters: 
      print("Hurray!") 
      letters.remove(letter) 
     else: 
      BODY_PARTS[wrongGuesses](HG) 
      wrongGuesses += 1 
      print("You have", MAXIMUM_WRONG_GUESSES - wrongGuesses, "guesses left!") 

def main(): 
    screen = Screen() 
    screen.setup(400, 400, 200, 200) 

    turtle = Turtle() 
    playAgain = True 

    print("Welcome to hangman!") 
    name = input("Enter your name: ") 
    print("Welcome", name + "!") 
    print("The rules are as follows:") 
    print(" 1) No looking at answer list!") 
    print(" 2) Do not have fun!!") 
    print(" 3) No hacking my game!") 

    tos = input("Do you accept the rules provided above? (yes or no): ") 

    if tos.lower().startswith("n"): 
     print("Who cares! The rules suck anyways!") 
    elif tos.lower().startswith("y"): 
     print("Hi") 
    else: 
     print("You don't listen very well. I guess proceed ugh") 

    while playAgain: 
     hangman(turtle) 

     answer = input('Do you want to play again? (yes or no): ') 
     playAgain = answer.lower().startswith("y") 

     if playAgain: 
      drawHangman.reset(turtle) 
     else: 
      print("Thanks for playing!") 

main()