TNieuwenhuis
TNieuwenhuis

Reputation: 1

Modular Guess the game loop

In my option 2 section of my code my loop isn't functioning properly and i can not figure out why. It keeps asking for an input again. I tried moving the input outside of the loop but that didn't work either.

 import random      

def display_menu():
        print("Welcome to my Guess the Number Program!")
        print("1. You guess the number")
        print("2. You type a number for the computer to guess.")
        print("3. Exit")
        print()

def main():
    display_menu()
    option = int(input("Enter a menu option: "))

User pics a number randomly generated by the computer until user gets the correct answer. Outputs user guesses and number of attempts until guessed correct

    if option == 1:

        number = random.randint(1,10)
        counter = 0

        while True:
            try:        
                guess = input("Guess a number between 1 and 10: ")
                guess = int(guess)
                print()
                if guess < 1 or guess > 10:
                    raise ValueError()
                counter += 1
                if guess > number:
                    print("Too high.")
                    print()
                elif guess < number:
                    print("Too low.")
                    print()
                else:
                    print("You guessed it!")
                    print("You guessed the number in", counter, "attempts!")
                    break
            except ValueError:
                print(guess, "is not a valid guess")
                print()

Option 2., User enters a number for the computer to guess. Computer guesses a number within the range given. Outputs computer guesses and number of guesses until computer gets the correct number.

    if option == 2:
        print("Computer guess my number")
        print()


        while True:
            try:
                my_num = input("Enter a number between 1 and 10 for the computer to guess: ")
                my_num = int(my_num)
                counter = 0
                counter += 1
                print()
                comp = random.randint(1,10)
                if my_num < 1 or my_num > 10:
                    raise ValueError()

                if comp > my_num:
                    print("Computer guessed", comp,"to High")

                elif comp < my_num:
                    print("Computer guessed", comp,"to Low")

                else:
                    print("Computer guessed the right number!" , comp)
                    print("Computer guessed the right number in", counter, "attempts!")
                    break

            except ValueError:
                print(my_num, "is not a valid entry")
                print()
                continue
    """
    Ends game
    """
    if option == 3:
        print("Goodbye")


if __name__ == '__main__':
    main()

Upvotes: 0

Views: 29

Answers (1)

TrebledJ
TrebledJ

Reputation: 8987

You should ask for input before the loop. counter should be initialised before the loop too.

if option == 2:
    print("Computer guess my number")
    print()

    # these three lines will be run once before the loop
    my_num = input("Enter a number between 1 and 10 for the computer to guess: ")
    my_num = int(my_num)
    counter = 0

    while True:
        try:
            comp = random.randint(1,10)
            counter += 1
            print()
            if my_num < 1 or my_num > 10:
                raise ValueError()

            if comp > my_num:
                print("Computer guessed", comp,"to High")

            elif comp < my_num:
                print("Computer guessed", comp,"to Low")

            else:
                print("Computer guessed the right number!" , comp)
                print("Computer guessed the right number in", counter, "attempts!")
                break

        except ValueError:
            print(my_num, "is not a valid entry")
            print()
            continue

As an aside, instead of randomly guessing, you can improve the computer's guessing by using binary search, where the number of tries has an upper bound.

Upvotes: 0

Related Questions