Mastermind在python输入验证和跟踪混淆

时间:2015-10-30 19:22:09

标签: python input

我在上课时遇到麻烦,需要一些帮助。我们必须制作一个mastermind程序,但它需要列出所做的猜测次数,用户给出的正确答案的变化以及包含用户有权利的提示以及他们在给出的答案中有多少次正确。最后,它需要列出他们猜测的数量。以下是我到目前为止的情况,出于某种原因,每当我尝试测试它时,整个事情都会锁定,我需要重新启动顶篷的核心。

import random

def mastermind():
    print 'working'
    count= 1
    s_code = []
    total_colors_right = 0
    total_in_right_pos = 0
    allguess = []
    theseguess = ()
    while count <6:
        ran_choice = random.choice('ryobg')
        s_code.append(ran_choice)
        count = count+1
        s_code.append(ran_choice)
        print s_code
        print "lets play a game"
        print "I the great and almighty OZ have selected a list of five colors choose wisely and their will be no consequences"
        print
        print "For each turn i will ask you to choose five colors. There are no rules I can choose the same color as many times as i want and so can you"
        print "Have the clors match mine and you will win  i wil give the occasional hint ahd the secret list of colors will stay the same for the entire length of our match"
        print "I will keep track of your colors and how many times you guess"
        print "i dont know why but im feeling generous so you can have an unlimited amount of guesses"
        print "if your feeling competitive have someone compare their score to yours and come back soon"
        print "have a good time, and don't anger me you won't like me when I'm angry"
        print "oh and by the way please just type your answer in one letter I don't feel like reading a whole word"
        not_correct = True
        while not_correct:
            position = 0
        guess_ctr= []
        for position in range(0,5):
            guess = raw_input('make your guess here genius >>')
            guess_ctr.append(guess)
        print "You guessed:",guess_ctr
        these_guesses = guess_ctr[0],guess_ctr[1],guess_ctr[2],guess_ctr[3],guess_ctr[3]
        print "these guesses are ",theseguess
        allguess.append(these_guesses)
        print "all your guesses are: ",allguess
        print total_colors_right,total_in_right_pos

        print "total colors right ",total_colors_right,",total in right position" 
        if total_in_right_pos == 5:
            not_correct = False
        else:
            not_correct = True
            print allguess
            print "well, I think that you've won"
            print
            print "well we cant have that now can we"
            print "have fun in umm... the... uhhh"
            print "the magic garden yeah thats definately a real place yup, have fun!"
            print
            print
            print "you were evaporated into a puff of greasy smoke"
            print 'goodbye, chump, have fun in the trashcan with the last one'

    def compare_lists(colors,secret,guess):
            print"secret list is",secret
            print"your list is",guess
            secret_red = secret.count('r')
            secret_orange = secret.count('o')
            secret_yellow = secret.count('y')
            secret_green = secret.count('g')
            secret_blue= secret.count('b')
            player_red = guess.count('r')
            player_orange = guess.count('o')
            player_yellow = guess.count('y')
            player_green = guess.count('g')
            player_blue= guess.count('b')
            total_colors_right = min(secret_red,player_red) -\
            min(secret_orange,player_orange) +min(secret_yellow,player_yellow)+\
            min(secret_green,player_green)+min(secret_blue,player_blue)
            print 'Total colors right: ',total_colors_right
            return total_colors_right

非常感谢任何和所有帮助!

1 个答案:

答案 0 :(得分:1)

R Nar解决了眼前的问题:你有一个简单的无限循环。问题是你的缩进:你显然希望 mastermind 例程的其余部分成为该循环的一部分。推开它,像这样:

    not_correct = True
    while not_correct:
        position = 0
        guess_ctr = []
        for position in range(0,5):
            guess = raw_input('make your guess here genius >>')
            guess_ctr.append(guess)
        print "You guessed:",guess_ctr
        these_guesses = guess_ctr[0],guess_ctr[1],guess_ctr[2],guess_ctr[3],guess_ctr[3]
        print "these guesses are ",theseguess
        allguess.append(these_guesses)
        print "all your guesses are: ",allguess
        print total_colors_right,total_in_right_pos

        print "total colors right ",total_colors_right,",total in right position"
        if total_in_right_pos == 5:
            not_correct = False
        else:
            not_correct = True
            print allguess
            print "well, I think that you've won"
            print
            print "well we cant have that now can we"
            print "have fun in umm... the... uhhh"
            print "the magic garden yeah thats definately a real place yup, have fun!"
            print
            print
            print "you were evaporated into a puff of greasy smoke"
            print 'goodbye, chump, have fun in the trashcan with the last one'

这引发了程序开发的频繁点。你是如何在没有注意到主游戏循环无限的情况下编写这么多代码的?

一次写几行。检查他们的操作。插入打印语句以跟踪程序流和变量值。当这些陈述有效时,然后添加更多。如果没有这种增量开发,你经常会发现自己处于这个位置:新代码的两个屏幕,没有线索。

接下来会注意到您的游戏结束逻辑已被撤消。它可能有助于简化您的逻辑。首先,将不是_ 放在正确前面,以便主循环读取

while not correct:

请注意,您在此处获得相同的效果 - 无论如何,Python否定运算符正是您想要的。在下面的决定中,你现在得到了

        if total_in_right_pos == 5:
            correct = True
        else:
            correct = False
            print allguess
            ...

或者,更直接(现在我们可以看到正确的逻辑)

        correct = total_in_right_pos == 5:
        if correct:
            print allguess
            ...

还有许多其他改进,更不用说对游戏文本的良好校对。但是,我会保持简洁和古老的格言:

首先让它发挥作用。

然后让它运作良好。

最后,让它漂亮。