andJusticeForAll
andJusticeForAll

Reputation: 15

Stop a loop inside function in Python 3

import random

def compareInput(n):
    if n == randomNumber: #Compares guess and the random number
        print("YAY!! you won the number was: " + n)
        return
        #(something) Loop should stop here.
    else:
        print("Wrong guess")



print("Guess the number") #Prints guess number duh!
randomNumber = str(random.randint(1,20)) #Generates a number between 1 and 20 and converts it to string
while True: #Input loop
    guess = str(input()) #Takes input and converts it to string
    if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
        compareInput(guess) #Call compareInput function
    else:
        print("Wrong input")

How do I stop the while loop?

Upvotes: 0

Views: 512

Answers (3)

Hugh Bothwell
Hugh Bothwell

Reputation: 56634

# assumes Python 3.x
import random

def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            # not an int, try again
            pass

def is_correct(guess, target):
    if guess == target:
        print("YAY!! you won, the number was {}".format(target))
        return True
    else:
        print("Wrong guess")
        return False

def main():
    print("Guess the number")
    target = random.randint(1,20)

    while True:
        guess = get_int("What's your guess?")
        if is_correct(guess, target):
            break

if __name__=="__main__":
    main()

Upvotes: 1

Leigh
Leigh

Reputation: 12506

You can use break to break the closest for or while loop. In your case, you'd need to check the return value of compareInput in the while loop and then break or return as required.

import random

def compareInput(n):
                if n == randomNumber: #Compares guess and the random number
                        print("YAY!! you won the number was: " + n)
                        return True
                        #(something) Loop should stop here.
                else:
                        print("Wrong guess")
                        return False



print("Guess the number") #Prints guess number duh!
randomNumber = str(random.randint(1,20)) #Generates a number between 1 and 20 and converts it to string
while True: #Input loop
        guess = str(input()) #Takes input and converts it to string
        if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
                if compareInput(guess): #Call compareInput function
                        break # We got the right guess!
        else:
                print("Wrong input")

Upvotes: 2

m.wasowski
m.wasowski

Reputation: 6387

while True: #Input loop
    guess = str(input()) #Takes input and converts it to string
    if len(guess) > 0 and guess.isdigit() : #input must be a number and at lest 1 digit
        compareInput(guess) #Call compareInput function
        break  ## you exit loop with break statement
    else:
        print("Wrong input")

Upvotes: 0

Related Questions