Home > Mobile >  My loop is running endlessly even though i have an end condition after i run the code
My loop is running endlessly even though i have an end condition after i run the code

Time:10-03

im doing a small "mastermind" game for a project, seems to run fine up until my last while loop, i thought i have an end statement but it seems to run on repeat. I've been stuck on this for some time now and i would appreciate any and all help on this, thanks! Here is the code:

import random

def generate_code():
    """Create a random code as a list"""

    for i in range(0,4):
        i = random.randint(0,5)
        code.append(i)
    print(code)
 

def make_guess(): 
    """Let's the user input a guess"""

    while len(guess) < 4:
        element = input("your guess, one at the time: " ) 
        if element.isnumeric():
            element = int(element)
            global amountOfGuesses
            if element in range(0,6):
                guess.append(element)
                amountOfGuesses = amountOfGuesses  1
            else:
                print("number has to be between 0 and 5")
        else:
            print("has to be a number between 0 and 5") 

def right_position(guess, code):
    """Calculate how many correkt numbers on right position the guess have"""
    howManyRight = 0

    for i in range(4):
        if guess[i] == code[i]:
            howManyRight = howManyRight  1
    return howManyRight

def wrong_position(guess, code): 
    """Calculate how many numbers are corret but wrong position"""
    howManyWrongPosition = 0
    tempCode = code[:]
    for i in guess:
        if i in tempCode:
            tempCode.remove(i)
            howManyWrongPosition = howManyWrongPosition  1

    howManyWrongPosition = howManyWrongPosition - right_position(guess, code)
    return howManyWrongPosition

code = []
guess = []
wrongPosition = []
rightPosition = []
codeCopy = code.copy()
amountOfGuesses = 0

print("Welcome to Mastermind.\nYou get seven guesses to gues a random 4 digit code with 6 different numbers between 0 and 5.")
generate_code()
while amountOfGuesses <= 7:
    make_guess()
    print("you have", right_position(guess, code), "right numbers on the right position")
    print("you have", wrong_position(guess, code), "numbers on that is right but on the wrong posiotion")
    if guess[:] == code[:]:
        print("Congratulation you won!!! you used", amountOfGuesses, "guesses.")

CodePudding user response:

From what I understand you want one try to be one input of 4 numbers, so I also fixed that. The reason you're getting an infinite loop is because you haven't broken out of the loop at end. You should also clear the guess array, otherwise the for loop inside the make_guess() will just skip due to the length being 4 (in case the guess was wrong and want to try again).

The fixed code (assuming one try is input of 4 numbers):

import random

def generate_code():
    """Create a random code as a list"""
    for i in range(0,4):
        i = random.randint(0,5)
        code.append(i)
    print(code)
 

def make_guess(): 
    """Let's the user input a guess"""
    global amountOfGuesses
    while len(guess) < 4:
        element = input("your guess, one at the time: " ) 
        if element.isnumeric():
            element = int(element)
            if element in range(0,6):
                guess.append(element)
            else:
                print("number has to be between 0 and 5")
        else:
            print("has to be a number between 0 and 5") 
    amountOfGuesses = amountOfGuesses  1

def right_position(guess, code):
    """Calculate how many correkt numbers on right position the guess have"""
    howManyRight = 0

    for i in range(4):
        if guess[i] == code[i]:
            howManyRight = howManyRight  1
    return howManyRight

def wrong_position(guess, code): 
    """Calculate how many numbers are corret but wrong position"""
    howManyWrongPosition = 0
    tempCode = code[:]
    for i in guess:
        if i in tempCode:
            tempCode.remove(i)
            howManyWrongPosition = howManyWrongPosition  1

    howManyWrongPosition = howManyWrongPosition - right_position(guess, code)
    return howManyWrongPosition

code = []
guess = []
wrongPosition = []
rightPosition = []
codeCopy = code.copy()
amountOfGuesses = 0

print("Welcome to Mastermind.\nYou get seven guesses to gues a random 4 digit code with 6 different numbers between 0 and 5.")
generate_code()
while 1:
    make_guess()
    print("you have", right_position(guess, code), "right numbers on the right position")
    print("you have", wrong_position(guess, code), "numbers on that is right but on the wrong posiotion")
    if guess == code:
        print("Congratulation you won!!! you used", amountOfGuesses, "guesses." if amountOfGuesses > 1 else "guess.")
        break
    elif amountOfGuesses > 7:
        print(f"You have lost by using {amountOfGuesses} tries!")
        break
    guess = []
  • Related