计划代码不会运行

2024-04-24 22:30:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个Mastermind代码,当我试图在我的Mac上运行它时,它只是说“注销”。如果有人知道原因,那将是非常有帮助的!代码如下:

def masterMind():  
    number = random.ranint(10000,99999) #the computer chooses 5 random numbers
    userGuess = raw_input("Guess my 5 digit password:") #asking the user to input their guess
    tries = 10 # telling the computer number of tries the user is allowed


    while tries < 0: # 10 attempts is the maximum amount 
        if number != userGuess: # if the computer's password does not equal the user's guess then it equals to one attempt
            tries += 1
            userGuess = input("Guess my 5 digit password:")
        else: #if the user and the computer's answers align 
            print "Win: ", userGuess
    print number 

Tags: theto代码numberinputifmyrandom
2条回答

更多的“Python”(2.x)方式。修正像“00000”这样的答案,将int修复到strcompair。在

def masterMind():  
    number = "%05d" % random.randint(0, 99999) #the computer chooses 5 random numbers
    for tries in range(10):
        user_guess = raw_input("Guess my 5 digit password:") #asking the user to input their guess
        if number == user_guess:
            print "Win on the %d try" % (tries + 1)
            break
    print "answer was:", number 
tries = 10
while tries < 0:

永远不会进入循环。在

您可能需要反转比较的意义,而使用>。在

您还需要在循环中递减tries,而不是递增它。在

相关问题 更多 >