Python 2.7,遇到迭代问题

时间:2015-09-21 17:29:06

标签: python-2.7

from __future__ import print_function
import random as rand

def ex_game():
    print('Instructions:','\n','Your job is to try to figure out a number of problems by your choosing and get the progress bar all GREEN, YELLOW means its there, but wrong spot, RED means its not there and there are no more of that left. After a light turns GREEN you do not have to get it right again. Trust me its easy.(Press enter to get started)')
    raw_input()
    ex_secret(int(raw_input('Number of items you would like to TRY to solve(WARNING: GAME GETS HARDER THE LARGER THE NUMBER YOU CHOOSE):')),int(raw_input('At what level would you like to play at.(1-5)')))
def ex_guess(l_sec,tries,cheat,number_items,level):
    #This is the guess choice I separated it so that I could rerun it as pleased, to put a special code in, and show the progression table
    guess_ex = []
    option_2 = ''
    option_3 = ''
    option_4 = ''
    option_5 = ''
    if level == 2 or level == 3 or level == 4 or level == 5:
         option_2 = ', white'
    if level == 3 or level == 4 or level == 5:
         option_3 = ', orange'
    if level == 4 or level == 5:
         option_4 = ', purple'
    if level == 5:
         option_5 = ', brown'
    for loop in range(number_items):
         append_guess = 'What do you beleive the solution is?(One at a time | choices: yellow, blue, black, green, red' + option_2 + option_3 + option_4 + option_5 +'):'
         guess_ex.append(raw_input(append_guess).lower())
    return guess_ex
def ex_secret(number_items,level):
    #Makes the random list of colors to solve
    secret_ex = []
    option_2 = ''
    option_3 = ''
    option_4 = ''
    option_5 = ''
    if level == 2 or level == 3 or level == 4 or level == 5:
        option_2 = ', white'
    if level == 3 or level == 4 or level == 5:
        option_3 = ', orange'
    if level == 4 or level == 5:
        option_4 = ', purple'
    if level == 5:
        option_5 = ', brown'
    for loop in range(number_items):
        secret_ex.append(rand.choice(['yellow','blue','black','green','red', option_2 , option_3, option_4, option_5]))
    for empty_check in secret_ex:
        if empty_check == '':
            ex_secret(number_items,level)
    report(secret_ex,[],0,False,number_items,level,False,False)
def report(secret,progress,tries,cheat,number_items,level,correct,end):
    #The "game"
    safe_secret = []
    for record in secret:
        safe_secret.append(record)
    if correct == True:
        print('YOU WON!')
        end = True
    if tries > 0 and cheat == False or tries > 4 and cheat:
        print(progress)
        progress = []
    if not end:
        while not correct:
            if tries == 5 and not correct:
                print('YOU LOST?!?!')
                end = True
                return end
            guess = ex_guess(len(secret),tries,cheat,number_items,level)
            super_secret = 'up up down down left right left right b a start'
            if 'give' in guess:
                tries = 4
            if super_secret in guess and not cheat:
                print('The deal has been made. Now make your "new" choices.')
                cheat = True
                tries += 6
                guess = ex_guess(len(secret),progress,tries,cheat,number_items,level)
            elif super_secret in guess and cheat:
                print('I said the deal was made now go make your new choices and leave me alone you prick.')
                guess = ex_guess(len(secret),progress,tries,cheat,number_items,level)
            elif 'stop_cheat' in guess and cheat:
                if tries > 8:
                    return 'YOU LOST, thank you for admitting to cheating though.'
                else:
                    cheat = False
                    tries -= 6
            list_counter_in = 0
            list_counter_out = 0
            list_counter_incor = 0
            while list_counter_in <= number_items - 1:
                if guess[list_counter_in] == secret[list_counter_in] and '_g' not in guess[list_counter_in]:
                    secret[list_counter_in] += ('_g')
                    guess[list_counter_in] += ('_g')
                list_counter_in += 1
            while list_counter_out <= number_items - 1:
                if guess[list_counter_out] in secret and '_g' not in guess[list_counter_out]:
                    secret_yellow_check = secret[secret.index(guess[list_counter_out])]
                    if '_y' not in secret_yellow_check:
                        secret[secret.index(guess[list_counter_out])] += '_y'
                        guess[list_counter_out] += '_y'
                list_counter_out += 1
            while list_counter_incor <= number_items - 1:
                if '_g' not in guess[list_counter_incor] and '_y' not in guess[list_counter_incor]:
                    guess[list_counter_incor] += '_r'
                list_counter_incor += 1
            for check_status in guess:
                if '_g' in check_status:
                    progress.append('GREEN')
                if '_y' in check_status:
                    progress.append('YELLOW')
                if '_r' in check_status:
                    progress.append('RED')
            if 'RED' not in progress and 'YELLOW' not in progress:
                correct = True
            if 'RED' in progress or 'YELLOW' in progress:
                tries +=1
                print('Not quite right...Lets try again!')
            report(safe_secret,progress,tries,cheat,number_items,level,correct,end)

基本上我对这段代码只有一个很大的问题,它一直在重复,在玩家获胜或失败之后重复可能有人帮我这个,是的,我尝试过使用返回。我基本上试图获得如果他们在随机秘密中获得所有正确的颜色,如果他们能够在他们输掉的5个动作中弄清楚它们就会赢得。

1 个答案:

答案 0 :(得分:0)

所以这里实际上有一些问题,它们主要归结为你在自己内部调用函数,这通常是一个非常糟糕的主意。我重写了你的功能,这样他们就不需要自己打电话了。这就是我想出的。 :)作为奖励,我添加了在播放时随时键入“退出”的选项(但在配置时实际输入并且在配置时输入“退出”会使游戏崩溃)以退出游戏。

from __future__ import print_function
import random as rand

def ex_game():
    print('Instructions:','\n','Your job is to try to figure out a number of problems by your choosing and get the progress bar all GREEN, YELLOW means its there, but wrong spot, RED means its not there and there are no more of that left. After a light turns GREEN you do not have to get it right again. Trust me its easy.(Press enter to get started)')
    raw_input()
    ex_secret(int(raw_input('Number of items you would like to TRY to solve(WARNING: GAME GETS HARDER THE LARGER THE NUMBER YOU CHOOSE):')),int(raw_input('At what level would you like to play at.(1-5)')))
def ex_guess(l_sec,tries,cheat,number_items,level):
    #This is the guess choice I separated it so that I could rerun it as pleased, to put a special code in, and show the progression table
    guess_ex = []
    colors = ['yellow', 'blue', 'black', 'green', 'red']

    if level >= 2:
        colors.append('white')
    if level >= 3:
        colors.append('orange')
    if level >= 4:
        colors.append('purple')
    if level >= 5:
        colors.append('brown')

    for loop in range(number_items):
         append_guess = 'What do you beleive the solution is?(One at a time | choices: ' + str(colors) + '):'
         input = raw_input(append_guess).lower()

         if input == 'quit':
            return 'quit'
         guess_ex.append(input)

    return guess_ex
def ex_secret(number_items,level):
    secret_ex = []
    optionalColors = []
    if level >= 2:
        optionalColors.append('white')
    if level >= 3:
        optionalColors.append('orange')
    if level >= 4:
        optionalColors.append('purple')
    if level >= 5:
        optionalColors.append('brown')

    for loop in range(number_items):
        secret_ex.append(rand.choice(['yellow','blue','black','green','red']+optionalColors))

    report(secret_ex,[],0,False,number_items,level,False)
def report(secret,progress,tries,cheat,number_items,level,correct):
    #The "game"
    safe_secret = []
    for record in secret:
        safe_secret.append(record)
    if correct == True:
        print('YOU WON!')
        end = True
    if tries > 0 and cheat == False or tries > 4 and cheat:
        print(progress)
        progress = []
    while not correct:
        if tries == 5 and not correct:
            print('YOU LOST?!?!')
            raw_input('Thanks for Playing!')
            return
        guess = ex_guess(len(secret),tries,cheat,number_items,level)
        super_secret = 'up up down down left right left right b a start'
        if 'give' in guess:
            tries = 4
        if super_secret in guess and not cheat:
            print('The deal has been made. Now make your "new" choices.')
            cheat = True
            tries += 6
            guess = ex_guess(len(secret),progress,tries,cheat,number_items,level)
        elif super_secret in guess and cheat:
            print('I said the deal was made now go make your new choices and leave me alone you prick.')
            guess = ex_guess(len(secret),progress,tries,cheat,number_items,level)
        elif 'stop_cheat' in guess and cheat:
            if tries > 8:
                return 'YOU LOST, thank you for admitting to cheating though.'
            else:
                cheat = False
                tries -= 6
        if guess == 'quit':
            raw_input('Thanks for Playing!')
            return
        list_counter_in = 0
        list_counter_out = 0
        list_counter_incor = 0
        while list_counter_in <= number_items - 1:
            if guess[list_counter_in] == secret[list_counter_in] and '_g' not in guess[list_counter_in]:
                secret[list_counter_in] += ('_g')
                guess[list_counter_in] += ('_g')
            list_counter_in += 1
        while list_counter_out <= number_items - 1:
            if guess[list_counter_out] in secret and '_g' not in guess[list_counter_out]:
                secret_yellow_check = secret[secret.index(guess[list_counter_out])]
                if '_y' not in secret_yellow_check:
                    secret[secret.index(guess[list_counter_out])] += '_y'
                    guess[list_counter_out] += '_y'
            list_counter_out += 1
        while list_counter_incor <= number_items - 1:
            if '_g' not in guess[list_counter_incor] and '_y' not in guess[list_counter_incor]:
                guess[list_counter_incor] += '_r'
            list_counter_incor += 1
        for check_status in guess:
            if '_g' in check_status:
                progress.append('GREEN')
            if '_y' in check_status:
                progress.append('YELLOW')
            if '_r' in check_status:
                progress.append('RED')
        if 'RED' not in progress and 'YELLOW' not in progress:
            correct = True
        if 'RED' in progress or 'YELLOW' in progress:
            tries +=1
            print('Not quite right...Lets try again!')
ex_game()