Home > Blockchain >  Guess Number with class and method
Guess Number with class and method

Time:03-27

I am trying to create a guess game using class/methods, but got stuck. When trying the code does not show me any errors but it does not print any of the conditionals created under the method. Can you please tell me what am I doing wrong?

class GuessProcessor:

    def __init__(self, my_secretNumber, user_numGuess):
        self.my_secretNumber = my_secretNumber
        self.user_numGuess = user_numGuess

    
    def compareGuess(self, user_numGuess, my_secretNumber):
        return compareGuess

        if user_numGuess >10 or user_numGuess <= 0:
            print("Invalid number. Try again!")

        elif user_numGuess < my_secretNumber:
            print("You guessed too low!")

        elif user_numGuess > my_secretNumber:
            print("You guessed too high")

my_secretNumber = int(6)

user_numGuess = int(input("Enter a number between 1 and 10: "))

The only thing that prints out is:

Enter a number between 1 and 10: 6

Process finished with exit code 0

CodePudding user response:

There are some flaws in your code.

First, your if-else logic won't be checked because you have return compareGuess

Second, you do not use the correct initialization variables when doing the if-else checking

Third, you don't have the condition when the guessing number is correct.

Fourth, you didn't initiate that GuessProcessor class to run the compareGuess with your "my_secretNumber" and "user_numGuess"

Fifth, start to read the coding guideline, especially for the naming convention (https://google.github.io/styleguide/pyguide.html#3162-naming-conventions)

Probably, you would like the code something like this

class GuessProcessor:

    def __init__(self, my_secret_number, user_num_guess):
        self.my_secret_number = my_secret_number
        self.user_num_guess = user_num_guess

    def compare_guess(self):
        if self.user_num_guess > 10 or self.user_num_guess <= 0:
            print("Invalid number. Try again!")

        elif self.user_num_guess < self.my_secret_number:
            print("You guessed too low!")

        elif self.user_num_guess > self.my_secret_number:
            print("You guessed too high")
        else:
            print("You guessed correctly!")


my_secret_number = int(6)

user_num_guess = int(input("Enter a number between 1 and 10: "))

GuessProcessor(my_secret_number, user_num_guess).compare_guess()

CodePudding user response:

You never thought about when the answer is right.

You need to add one more elif statement:

elif user_num_guess == my_secretNumber:
   print("Your right!")

Then, you can compare, if you want an response back from Python.

test = GuessProcessor(my_secretNumber, user_num_guess)
test.compare_guess()

The full code will look like this:

class GuessProcessor:

def __init__(self, my_secretNumber, user_numGuess):
    self.my_secretNumber = my_secretNumber
    self.user_numGuess = user_numGuess


def compareGuess(self, user_numGuess, my_secretNumber):
    return compareGuess

    if user_numGuess >10 or user_numGuess <= 0:
        print("Invalid number. Try again!")

    elif user_numGuess < my_secretNumber:
        print("You guessed too low!")

    elif user_numGuess > my_secretNumber:
        print("You guessed too high")

    elif user_num_guess == my_secretNumber:
        print("Your right!")

my_secretNumber = int(6)

user_numGuess = int(input("Enter a number between 1 and 10: "))

test = GuessProcessor(my_secretNumber, user_num_guess)
test.compare_guess()

By the way, couldn't you have just used an function instead of an class and add random?

  • Related