Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Just starting to learn Python, and encounter a problem

58 views
Skip to first unread message

Zagyen Leo

unread,
Jul 22, 2016, 10:00:34 AM7/22/16
to
yeah, it may be quite simple to you experts, but hard to me.

In one of exercises from the Tutorial it said: "Write a program that asks the user their name, if they enter your name say "That is a nice name", if they enter "John Cleese" or "Michael Palin", tell them how you feel about them ;), otherwise tell them "You have a nice name."

And i write so:

name = input("Enter your name here: ")
if name == "John Cleese" or "Michael Palin":
print("Sounds like a gentleman.")
else:
print("You have a nice name.")

But strangely whatever I type in (e.g. Santa Claus), it always say "Sounds like a gentleman.", not the result I want.

Random832

unread,
Jul 22, 2016, 10:30:23 AM7/22/16
to
"or" is a lower precedence than "==".

> if name == "John Cleese" or "Michael Palin"

becomes if (name == "John Cleese") or "Michael Palin"; becomes if False
or "Michael Palin"; becomes if "Michael Palin"; and non-empty strings
are considered true.

You want if Name == "John Cleese" or Name == "Michael Palin"; or if Name
in ("John Cleese", "Michael Palin")

Bob Gailer

unread,
Jul 22, 2016, 10:36:46 AM7/22/16
to
Even without knowing the operator precedence, this will be evaluated either
as:
(name == "John Cleese") or "Michael Palin")
or:
name == ("John Cleese" or "Michael Palin").

Case 1: (name == "John Cleese") evaluates to either True or False. False
or "Michael Palin" evaluates to ( believe it or not) " Michael Palin"!
Which, as far as if is concerned, is True. True or "Michael Palin"
evaluates to True.

Case 2: "John Cleese" or "Michael Palin" evaluates to False; name== False
evaluates to False.

One way to get the results you want:
if name in ("John Cleese" or "Michael Palin"):

Gordon Levi

unread,
Jul 22, 2016, 10:37:45 AM7/22/16
to
The second line should be
if name == "John Cleese" or name == "Michael Palin":

As discussed in recent lengthy thread in this group the following
line, and hence your statement, is always true -

If "Michael Palin":

justin walters

unread,
Jul 22, 2016, 11:04:58 AM7/22/16
to
:
> --
> https://mail.python.org/mailman/listinfo/python-list

The easiest way to right this would be to use a tuple like so:

if name in ('John Cleese', 'Michael Palin'):
print ('They sound like a gentleman')

MRAB

unread,
Jul 22, 2016, 11:08:31 AM7/22/16
to
This bit:

name == "John Cleese" or "Michael Palin"

means the same as:

(name == "John Cleese") or "Michael Palin"

If name is "Santa Claus", that's:

"Santa Claus" == "John Cleese" or "Michael Palin"

which is:

False or "Michael Palin"

which is:

"Michael Palin"

and any string except "" is treated as True.

The condition should be:

name == "John Cleese" or name == "Michael Palin"

(Shorter alternatives are available; you'll learn about them later!)

Zagyen Leo

unread,
Jul 23, 2016, 2:14:19 AM7/23/16
to
I got it! Thank you.

Hope in one day I could help other newbies as you do.

gst

unread,
Jul 23, 2016, 2:19:19 PM7/23/16
to
Heuh case 2 :

"String1" or "String2"

Evaluates to "String1" ?

MRAB

unread,
Jul 23, 2016, 3:06:52 PM7/23/16
to
Suppose you have:

x or y

If bool(x) returns True, then the result will be x, else the result will
be y.

Example 1:

bool("String1") returns True, therefore the result of:

"String1" or "String2"

is "String1".

Example 2:

bool("") returns False, so the result of:

"" or "String2"

is "String2".

(The empty string "" is considered 'false-y'; all other strings (i.e.
all non-empty strings) are considered 'true-y'.)

Steven D'Aprano

unread,
Jul 23, 2016, 9:59:07 PM7/23/16
to
Correct. What did you expect?

Have you read the Fine Manual?


https://docs.python.org/3/reference/expressions.html#boolean-operations



--
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

0 new messages