为什么其他方法无法正常工作?

时间:2017-10-13 22:13:06

标签: python

我正在为一堂课做一个简单的刽子手游戏。即使用户得到正确答案,当前程序也会完成图纸的每一步。

additionalData

2 个答案:

答案 0 :(得分:2)

问题  在你的/正确的决定逻辑中:

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

for 遍历单词的字符。例如,如果单词是“过度”,则每次猜测都会执行此循环七次次。即使玩家做出最好的猜测,“e”,你也会对他/她的字母“ovrat”进行错误的猜测,并画出五个男孩的部分。

<强> REPAIR

首先,您必须确定最近猜到的字母是否在中的任何地方

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

还有其他问题和改进,但这应该让你感动。

答案 1 :(得分: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()