2017-10-13 31 views
1

私はクラスのための簡単なハングマンゲームを作っています。現在のプログラムは、たとえユーザーが正しい答えを得たとしても、図面のすべてのステップを実行します。なぜelseが正しく動作しないのですか?

import turtle 
import random 
import sys 

while True: 
    list = ['report', 'beach', 'mayor', 'score', 'overeat', 'load', 'battery', 'social', 'honor', 'risk'] 

    turns = 10 
    guesses = '' 
    err = 0 

    usedw = random.choice(list) 

    wn = turtle.Screen() 
    draw = turtle.Turtle() 

    length = len(usedw) 

    print("Welcome to hangman your word is", length, "letters long.") 

    while turns > 0: 
     guess = input("Choose a character: ") 
     guesses += guess; 
     lengthr = 0 
     for char in usedw: 
      if char in guesses: 
       print (char,) 
       lengthr += 1; 
      else: 
       err += 1; 
       if err == 1: 
        draw.goto(0,0) 
        draw.down() 
        draw.goto(200,0) 
       elif err == 2: 
        draw.up() 
        draw.goto(0,0) 
        draw.down() 
        draw.goto(0,200) 
       elif err == 3: 
        draw.up() 
        draw.goto(0,200) 
        draw.down() 
        draw.goto(100,200) 
       elif err == 4: 
        draw.up() 
        draw.goto(100,200) 
        draw.down() 
        draw.goto(100,150) 
       elif err == 5: 
        draw.up() 
        draw.goto(100,100) 
        draw.down() 
        draw.circle(25) 
       elif err == 6: 
        draw.up() 
        draw.goto(100,100) 
        draw.down() 
        draw.goto(100,50) 
       elif err == 7: 
        draw.up() 
        draw.goto(75,90) 
        draw.down() 
        draw.goto(125,90) 
       elif err == 8: 
        draw.up() 
        draw.goto(100,50) 
        draw.down() 
        draw.goto(75,35) 
       elif err == 9: 
        draw.up() 
        draw.goto(100,50) 
        draw.down() 
        draw.goto(125,35) 
        print("You loose.") 
        break 
      if lengthr == length: 
       print ("You won!") 
       break 
     cont = input("Would you like to continue (y/n) ") 
     if cont == 'y': 
      print("Alright!") 
     else: 
      print("Thanks for playing!") 
      break 
      sys.exit() 
+0

あなたが推測していなかったキャラクターごとに1度詠唱しています。 – user2357112

答えて

0
import turtle 
import random 
import sys 

while True: 
    list = ['report', 'beach', 'mayor', 'score', 'overeat', 'load', 'battery', 'social', 'honor', 'risk'] 

    turns = 10 
    guesses = set() 
    err = 0 

    usedw = random.choice(list) 

    wn = turtle.Screen() 
    draw = turtle.Turtle() 

    length = len(usedw) 
    p_length = 0 
    c_length = 0 

    print("Welcome to hangman your word is", length, "letters long.") 

    while turns > 0: 
     guess = input("Choose a character: ") 
     # 'previous' guesses length 
     p_length = len(guesses) 
     # Adds unused guesses only 
     guesses.add(guess) 
     # 'current' guesses length 
     c_length = len(guesses) 
     # detect UNIQUE guess entry and guess in usedw 
     if (p_length != c_length and guess in usedw): 
      for char in usedw: 
       if guess == char: 
        print (char,) 
        # decrement remaining chars to match 
        length -= 1 
     else: # either a duplicate/unmatched guess 
      err += 1 
      if err == 1: 
       draw.goto(0,0) 
       draw.down() 
       draw.goto(200,0) 
      elif err == 2: 
       draw.up() 
       draw.goto(0,0) 
       draw.down() 
       draw.goto(0,200) 
      elif err == 3: 
       draw.up() 
       draw.goto(0,200) 
       draw.down() 
       draw.goto(100,200) 
      elif err == 4: 
       draw.up() 
       draw.goto(100,200) 
       draw.down() 
       draw.goto(100,150) 
      elif err == 5: 
       draw.up() 
       draw.goto(100,100) 
       draw.down() 
       draw.circle(25) 
      elif err == 6: 
       draw.up() 
       draw.goto(100,100) 
       draw.down() 
       draw.goto(100,50) 
      elif err == 7: 
       draw.up() 
       draw.goto(75,90) 
       draw.down() 
       draw.goto(125,90) 
      elif err == 8: 
       draw.up() 
       draw.goto(100,50) 
       draw.down() 
       draw.goto(75,35) 
      elif err == 9: 
       draw.up() 
       draw.goto(100,50) 
       draw.down() 
       draw.goto(125,35) 
       print("You loose.") 
       break 

     # detect completion 
     if 0 == length: 
      print ("You won!") 
      break 

    cont = input("Would you like to continue (y/n) ") 
    if cont == 'y': 
     print("Alright!") 
    else: 
     print("Thanks for playing!") 
     break 
     sys.exit() 
3

PROBLEM は、あなたの中に/正しい決意ロジックである:単語の文字による反復処理するための

for char in usedw: 
    if char in guesses: 
     print (char,) 
     lengthr += 1; 
    else: 
     err += 1; 
     if err == 1: 
      ... 

。たとえば、単語が「誇張」である場合、このループをの7回の実行ごとに実行します(それぞれの推測では回)。たとえプレーヤーが "e"を最もよく推測したとしても、 "ovrat"という文字の間違った推測をして、5人の男の子の部分を引きます。


修理

まず、あなたが最も最近推測文字が単語どこでもであるかどうかを判断する必要があります。

if guess in usedw: 
    print (char,) 
    lengthr += 1; 
else: 
    err += 1; 
    if err == 1: 
    ... 

あり、他の問題や改善点がありますが、このあなたを動かすはずです。

関連する問題