Home > Software design >  Execute code only when for loop is finished
Execute code only when for loop is finished

Time:09-22

I'm stuck on my homework assignement.

I made a guessing game in Python, everything works like it should be except 1 thing.

def main(random_pick, guesses):
for i in range(guesses):
        try:
            guess = int(input(f"{BLUE}[ ? ]{WHITE} Guess the number : "))
            if guess > random_pick:
                print(f"{RED}[ ! ]{WHITE} Your guess is high!")
            elif guess < random_pick:
                print(f"{RED}[ ! ]{WHITE} Your guess is low")
            else:
                print(f"{GREEN}[ $ ]{WHITE} You guessed it! Congratulations")
                break
        except ValueError:
            print(f"{RED}[ ! ]{WHITE} Wrong value, try again!")
print(f"{YELLOW}[ ! ]{WHITE} Better luck next time, the number was {random_pick}")

The last print lets the user know that he or she is out of guesses and shows the random number. The problem is when the user guesses the number right, that print also gets executed.

Output.

[ ? ] Enter an integer from 1 to 100 : 1
[ ? ] Enter another integer from 1 to 100 : 2
[ ? ] How many times do you want to guess? : 3
The answer = 2
[ ? ] Guess the number : 2
[ $ ] You guessed it! Congratulations
[ ! ] Better luck next time, the number was 2

I tried messing with the placement of the last print, some tabs forward or backwards but this doesn't seem to help either. Can anyone explain to me what i'm doing wrong?

Kind regards,

Sleek

CodePudding user response:

The break just breaks the loop. Because the print is after the loop, it is still executed. You should maybe completely return out of the function if you don't want the print to be executed.

def main(random_pick, guesses):
    for i in range(guesses):
        try:
            guess = int(input(f"{BLUE}[ ? ]{WHITE} Guess the number : "))
            if guess > random_pick:
                print(f"{RED}[ ! ]{WHITE} Your guess is high!")
            elif guess < random_pick:
                print(f"{RED}[ ! ]{WHITE} Your guess is low")
            else:
                print(f"{GREEN}[ $ ]{WHITE} You guessed it! Congratulations")
                # return instead of break
                return
        except ValueError:
            print(f"{RED}[ ! ]{WHITE} Wrong value, try again!")
    print(f"{YELLOW}[ ! ]{WHITE} Better luck next time, the number was {random_pick}")

CodePudding user response:

All you needed was to replace break with a return. The 'break' statement only lets you get out of the closest enclosing loop ('for' loop or 'while' loop). The 'return' statement exits the function entirely, which is what you seem to be looking for.

def main(random_pick, guesses):
    for i in range(guesses):
        try:
            guess = int(input(f"{BLUE}[ ? ]{WHITE} Guess the number : "))
            if guess > random_pick:
                print(f"{RED}[ ! ]{WHITE} Your guess is high!")
            elif guess < random_pick:
                print(f"{RED}[ ! ]{WHITE} Your guess is low")
            else:
                print(f"{GREEN}[ $ ]{WHITE} You guessed it! Congratulations")
                return
        except ValueError:
            print(f"{RED}[ ! ]{WHITE} Wrong value, try again!")
    print(f"{YELLOW}[ ! ]{WHITE} Better luck next time, the number was {random_pick}")
  • Related