Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The code inside my Python 3.9.13 "if-statement" is not being ran, even though the condition seems to be true

My code is intended to update wins, ties, and losses in accordance to rock paper scissors rules, but does not.

I am very confused, as printing CLASS gives the output "s", but comparing CLASS to "s" returns False.
Some code is omitted, and the omitted code makes no reference to wins, losses, ties, CLASS, or class_name

Code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
wins = 0
losses = 0
ties = 0
while True:
    save_webcam_image()
    image = Image.open(file_path).convert("RGB")
    size = (224, 224)
    image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
    image_array = np.asarray(image)
    normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
    data[0] = normalized_image_array
    prediction = model.predict(data, verbose=0)
    index = np.argmax(prediction)
    class_name = class_names[index]
    confidence_score = prediction[0][index]

    CLASS = str(class_name[2:])
    botChoice = random.choice(['rock', 'paper', 'scissors'])
    print("You choose:",CLASS)
    print("I choose:",botChoice)

    if CLASS == "r":
        print("rock registered")

        if botChoice == "rock":
            ties += 1
        elif botChoice == "paper":
            losses += 1
        else:
            wins +=1

    elif CLASS == "p":
        print("paper registered")

        if botChoice == "paper":
            ties += 1
        elif botChoice == "scissors":
            losses += 1
        else:
            wins +=1

    elif CLASS == "s":
        print("scissors registered")
        if botChoice == "scissors":
            ties += 1
        elif botChoice == "rock":
            losses += 1
        else:
            wins +=1

            

    print("\nCurrent Score: "+str(wins)+" / " + str(ties) + " / " + str(losses) + "\n\n")

Output:

You choose: s

I choose: paper

Current Score: 0 / 0 / 0

I expected that, since print(CLASS) prints "s", CLASS == "s" should be true, but it is not. Only the very bottom print statement is being run, and "rock/paper/scissors registered" never prints no matter what the class is. The issue isn’t just when CLASS == "s". When it is "p" or "r" the print statement which tells if the score was updated: print("rock/paper/scissors registered"), never runs. Why is this, and how can i fix it?

>Solution :

I could not reproduce your error because some objects are missing. But are you sure there is no whitespace in your string? ‘s’ is not the same as ‘s ‘ or ‘ s’. You can use str.strip for that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading