用Python制作主脑

用Python制作主脑,python,python-2.x,Python,Python 2.x,我只是想知道我如何才能让我的智囊团游戏工作,更具体地说,我将如何着手使“finish”全局化,我将如何调用这些函数以使程序正常工作,以及有助于增强代码的总体提示。我还想知道如何使程序不会在每个循环中“重新滚动”计算机生成的数字。这对我来说只是一个难题,我似乎不明白如何用正确的函数调用和像这样的利基方面来结束它。多谢各位 run = True def get_guess(): while run: guess = input("Provide four unique nu

我只是想知道我如何才能让我的智囊团游戏工作,更具体地说,我将如何着手使“finish”全局化,我将如何调用这些函数以使程序正常工作,以及有助于增强代码的总体提示。我还想知道如何使程序不会在每个循环中“重新滚动”计算机生成的数字。这对我来说只是一个难题,我似乎不明白如何用正确的函数调用和像这样的利基方面来结束它。多谢各位

run = True

def get_guess():
    while run:
        guess = input("Provide four unique numbers: ")
        count = 0
        if len(guess) == 4:    
            guessdupe = guess[0] == guess[1] or guess[0] == guess[2] or guess[0] == guess[3] or guess[1] == guess[2] or guess[1] == guess[3] or guess[2] == guess[3]
        else:
            guessdupe = False
        try:
            try:    
                for i in range(4):
                    if int(guess[i]) <= 7 and int(guess[i]) >= 1 and len(guess) == 4:
                        count += 1
                if len(guess) != 4:
                    print "Your guess must consist of 4 numbers!"
                if guessdupe:
                    count -= 1
                    print "You can only use each number once!"
            except ValueError:
                print "You can only use numbers 1-7 as guesses"
        except IndexError:
            print "You can only use numbers 1-7 as guesses"
        if count == 4:
            break
    return guess

def check_values(computer_list, user_list):
    final_list = [''] * 4
    for i in range(4):    
        if user_list[i] in computer_list:
            if user_list[i] == computer_list[i]:
                final_list[i] = "RED"
            else:
                final_list[i] = "WHITE"
        else:
            final_list[i] = "BLACK"
    random.shuffle(final_list)
    print final_list
    return final_list



def check_win(response_list):
    if response_list[0] == "RED" and response_list[1] == "RED" and response_list[2] == "RED" and response_list[3] == "RED":
        print "Congratulations! You've won the game!"
        global finish
        finish = True



def create_comp_list():
    while run:
        compList = [random.randint(1, 7), random.randint(1, 7), random.randint(1, 7), random.randint(1, 7)] 
        listBool = compList[0] == compList[1] or compList[0] == compList[2] or compList[0] == compList[3] or compList[1] == compList[2] or compList[1] == compList[3] or compList[2] == compList[3]
        if listBool:
            continue
        else:
            return compList



def play_game():
    for i in range(5):
        print create_comp_list()
        print get_guess()
        check_win(check_values(create_comp_list(), get_guess()))
        if finish:
            break


play_game()```
run=True
def get_guess():
运行时:
猜测=输入(“提供四个唯一的数字:”)
计数=0
如果len(猜测)==4:
guessdupe=猜测[0]==猜测[1]或猜测[0]==猜测[2]或猜测[0]==猜测[3]或猜测[1]==猜测[2]或猜测[1]==猜测[3]或猜测[2]==猜测[3]
其他:
猜错
尝试:
尝试:
对于范围(4)中的i:
如果int(猜测[i])=1且len(猜测)==4:
计数+=1
如果len(猜)!=4:
打印“您的猜测必须由4个数字组成!”
如果猜测重复:
计数-=1
打印“每个数字只能使用一次!”
除值错误外:
打印“您只能使用数字1-7作为猜测”
除索引器外:
打印“您只能使用数字1-7作为猜测”
如果计数=4:
打破
返回猜测
def检查值(计算机列表、用户列表):
最终清单=['']*4
对于范围(4)中的i:
如果计算机列表中的用户列表[i]:
如果用户列表[i]==计算机列表[i]:
最终列表[i]=“红色”
其他:
最终列表[i]=“白色”
其他:
最终列表[i]=“黑色”
随机。洗牌(最终列表)
打印最终用户列表
返回最终用户列表
def check_win(响应列表):
如果响应列表[0]=“红色”和响应列表[1]=“红色”和响应列表[2]=“红色”和响应列表[3]=“红色”:
打印“祝贺你!你赢得了比赛!”
整体光洁度
完成=真
def create_comp_list():
运行时:
compList=[random.randint(1,7),random.randint(1,7),random.randint(1,7),random.randint(1,7)]
listBool=compList[0]==compList[1]或compList[0]==compList[2]或compList[0]==compList[3]或compList[1]==compList[2]或compList[1]==compList[3]或compList[2]==compList[3]
如果是listBool:
持续
其他:
返回compList
def play_game():
对于范围(5)中的i:
打印创建组件列表()
打印get_guess()
check_win(检查_值(创建_comp_list(),获取_guess()))
如果完成:
打破
玩游戏```