Home > front end >  How to loop a simple game to continue until user stops it not using a break?
How to loop a simple game to continue until user stops it not using a break?

Time:12-09

def set_number():
    import random
    return random.randint(1,500)

    #This function plays the game
def number_guessing_game(number):
    guess_counter = 0
    guess = int(input("Enter a number between 1 and 500."))
    while guess != number:
        guess_counter  = 1
        if guess > number:
            print(f"You guessed too high. Try Again!")
            guess = int(input("Enter a number between 1 and 500."))
        elif guess < number:
            print(f"You guessed too low. Try Again!")
            guess = int(input("Enter a number between 1 and 500."))
    if guess == number:
        print(f"You guessed the number! Good Job.!")
        again = str(input("would you like to play again? Enter 'y' for yes or 'n' to close the game."))

def main():
    print(f"Welcome to the Number Guessing Game!\n"  
    f"You will have unlimited guesses. The number is between 1 and 500.\n"  
    f"Good Luck!")
    number = set_number()
    guess_count = number_guessing_game(number)
main()

I am working on a simple game project for my coding class. I am not good at coding at all. I came up with this part of the program, I just cannot figure out how to loop the entire number_guessing_game function until the user enters 'n' to stop it, I can't use a break because we did not learn it in the class and I will receive a 0 if I use a break.

I tried nesting a while loop inside of the function but I know I did it wrong.

CodePudding user response:

Instead of using break use return.

def main():
    print(f"Welcome to the Number Guessing Game!\n"  
    f"You will have unlimited guesses. The number is between 1 and 500.\n"  
    f"Good Luck!")
    while True:
        number = set_number()
        number_guessing_game(number)
        again = input("would you like to play again? Enter 'y' for yes or 'n' to close the game.")
        if again == 'n':
            return
main()

You will probably want to remove the last line of the number_guessing_game function if you use this approach

CodePudding user response:

First, your code is assuming the return of input is an integer that can be converted with int(). If you were to give it 'n' your program will crash. Instead you could use the string class method isdigit() to see if the input was an integer value and then make a logical decision about it. Also note you do not need to convert the return from input to a str() as it is already a str type. You can confirm this with a print(type(input("give me something")))

guess = input("Enter a number between 1 and 500. 'n' to quit"))
if guess.isdigit():
   [your code to check the value]
elif ('n' == guess):
   return
else:
   print(f"You entered an invalid entry: {guess}. Please re-enter a valid value")

If you dont like the idea of using 'return' you could change your while loop to look something like:

while(('n' != guess) or (guess != number)):

If you want the function body looping continuously you could have some code like:

def number_guessing_game(number):
    exit_game = False
    guess_counter = 0
    while(exit_game != True):
      guess = input("Enter a number between 1 and 500.))
      guess_counter  = 1
      if guess.isdigit():
        if int(guess) > number:
            print("You guessed too high. Try Again!")
        elif int(guess) < number:
            print("You guessed too low. Try Again!")
        elif int(guess) == number:
            print("You guessed the number! Good Job.!")
        again = input("would you like to play again? Enter 'y' for yes or 'n' to close)
        if ('n' == again):
           exit_game = True
      else:
         print("Error, please enter a valid value")
  • Related