问题描述:

This question already has an answer here:

  • Python always thinks my string is the highest possible number [duplicate]

    6 answers

网友答案:
user == "paper" or "Paper"

is always true. The or operator tests the expressions on either side of itself, and if either is true, the result of the or is also true. Your test above checks (up to) two things:

  • Is the expression user == "paper" true? If so, the whole expression is true, so don't check the second part, because True or x is always true regardless of the value of x.
  • Is the expression "Paper" true? And because non-zero-length strings are true in Python, this part is always true.

So even if the first part is false, the second part is always true, so the expression as a whole is always true.

You wanted something like this:

user == "paper" or user == "Paper"

or, better yet:

user in ("paper", "Paper")

or, best of all:

user.lower() == "paper"
网友答案:

You can also do this with lists and in:

if user in ["paper", "Paper"]:
    paper()

or using regex:

import re
user = 'paper'
if re.match('papers?', user):
    paper()
elif re.match('[Rr]ock', user):
    rock()

with regexes you can also do case-insensitive match:

import re
user = 'paper'
if re.match('papers?', user, re.I):
    paper()

which will match all: paper, PapER, PaperS, ...

网友答案:

you want:

if user == "paper" or user == "Paper":

Same for the others as well.

If you just put

if "Paper":

Python evaluates it as if this_value_is_true. Same with the code you have basically evaluates to "if user variable equals 'paper' or True" which would always be tue.

网友答案:

I believe I know where your problem comes from:

if user == "paper" or user == "Paper":

That should fix the problem

网友答案:

This:

if user == "paper" or "Paper":

… is parsed as this:

if (user == "paper") or "Paper":

If user actually is equal to "paper", that's if True or "Paper", which is True.

Otherwise, that's if False or "Paper", which is "Paper".

Since True and "Paper" are both truthy, the if always happens.

网友答案:
if user == "paper" or "Paper":

Actually evaluates to:

(user == "paper")  or "Paper"

i.e (result of user == "paper") or "Paper"

So the 2 possibilities here are:

  1. True or "Paper"
  2. False or "Paper"

In first case it returns True and in the second one it returns "Paper".

As "Paper" is a True value(all non-empty strings are True) so this condition is always True.

You should use:

if user.lower() == "paper"
相关阅读:
Top