0

I am trying to make a "guess the word" type game. Right now I have made two classes, each one for the computers random number (called cpu_input in the code). And a class for the user input, called user_input in the code.

class UserInput:
    def __init__(self, useri):
        self.useri = useri

    def get_char1(self):
        return self.useri[0]

    def get_char2(self):
        return self.useri[1]

    def get_char3(self):
        return self.useri[2]

The goal here is to have a three letter word generated, and have each character from the user input and the random word be accessible. For the purpose I need, this part is working fine.

The second class is identical to this one. Just for the randomly generated number.

class CpuInput:
    def __init__(self, cpui):
        self.cpui = cpui

    def get_char1(self):
        return self.cpui[0]

    def get_char2(self):
        return self.cpui[1]

    def get_char3(self):
        return self.cpui[2]

As before, this class is working perfectly fine, for its purpose in my program.

However, I run into a problem when I want to start comparing the user_input to the cpu_input. This is what I have written:

cpu_input = CpuInput("Jet")
user_input = UserInput(input("Place user input here: "))

if cpu_input == user_input:
    print("Guessed correctly.")

Here I have set the cpu_input to ALWAYS be Jet for the sake of testing, whenever I enter Jet in the user input the program just comes to an end. Like, it just skips over the if completely?: This is the output:

Place user input here: Jet

Process finished with exit code 0

Am I doing something obviously wrong? Or a concept I'm just completely missing from my program? I really don't understand why the if statement isn't happening.

James
  • 3,252
  • 1
  • 18
  • 33

1 Answers1

1

You create two instances of (different) classes. They are not equal - you can verify this by printing id(cpu_input) == id(user_input).

Even if both where instances of the same class - they are different object in memory with unique id(..)'s - hence not equal.

A class is a concept of something - like "a tree" - choose any 2 trees (different or same species "aka class") - they are not equal - unless you pick the same tree twice.

You store the values inside the classes - you can compare the class instance values you stored:

if cpu_input.cpui == user_input.cpui:  
    print("Guessed correctly.")

Adding a

else:
    print("Your comparison is not equal!")

will make it more clear when it is not the same.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
JustANoob
  • 580
  • 1
  • 4
  • 19