Python 3.9.13中我的“if语句”内部的代码没有运行,尽管条件似乎为真。

huangapple go评论67阅读模式
英文:

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

问题

I apologize for any confusion. It appears that your code includes HTML-encoded characters like " and ', which might be causing the unexpected behavior when comparing CLASS to "s", "p", or "r". You should use regular double or single quotes (", ') instead of the HTML-encoded characters. Here's the corrected code:

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")  # Use regular double quotes here
    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'])  # Use regular single quotes here
    print("You choose:", CLASS)
    print("I choose:", botChoice)

    if CLASS == "r":  # Use regular double quotes here
        print("rock registered")

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

    elif CLASS == "p":  # Use regular double quotes here
        print("paper registered")

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

    elif CLASS == "s":  # Use regular double quotes here
        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")

By using regular quotes, your code should work as expected, and the comparisons with "s", "p", and "r" should yield the correct results.

英文:

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:

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?

答案1

得分: 0

停止考虑愚蠢的想法,尝试打印意外的CLASS值

if CLASS == "r":
    print("rock registered")
elif CLASS == "p":
    print("paper registered")
elif CLASS == "s":
    print("scissors registered")
else:
    print(f"unexpected {CLASS=}")
英文:

Cease pondering foolish ideas, try to print unexpected CLASS value

if CLASS == "r":
print("rock registered")
elif CLASS == "p":
print("paper registered")
elif CLASS == "s":
print("scissors registered")
else:
print(f"unexpected {CLASS=}")

答案2

得分: -1

我无法重现您的错误,因为一些对象丢失了。但您确定您的字符串中没有空格吗?'s'不同于's '' s'。您可以使用str.strip来处理这个问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月28日 15:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定