Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111753
| From | Bob Gailer <bgailer@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Just starting to learn Python, and encounter a problem |
| Date | 2016-07-22 10:36 -0400 |
| Message-ID | <mailman.50.1469198194.22221.python-list@python.org> (permalink) |
| References | <ac35006b-ab2e-44f1-b896-cd57d9689c73@googlegroups.com> <CAP1rxO6F0q2dOd1T_wV0=PNN6Gny1wFDQ3GUw5Js-7pTaEwREQ@mail.gmail.com> |
On Jul 22, 2016 10:00 AM, "Zagyen Leo" <zagyen@gmail.com> wrote:
>
> 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.
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"):
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Just starting to learn Python, and encounter a problem Zagyen Leo <zagyen@gmail.com> - 2016-07-22 06:59 -0700
Re: Just starting to learn Python, and encounter a problem Random832 <random832@fastmail.com> - 2016-07-22 10:30 -0400
Re: Just starting to learn Python, and encounter a problem Zagyen Leo <zagyen@gmail.com> - 2016-07-22 23:13 -0700
Re: Just starting to learn Python, and encounter a problem Bob Gailer <bgailer@gmail.com> - 2016-07-22 10:36 -0400
Re: Just starting to learn Python, and encounter a problem gst <g.starck@gmail.com> - 2016-07-23 11:18 -0700
Re: Just starting to learn Python, and encounter a problem MRAB <python@mrabarnett.plus.com> - 2016-07-23 20:06 +0100
Re: Just starting to learn Python, and encounter a problem Steven D'Aprano <steve@pearwood.info> - 2016-07-24 11:58 +1000
Re: Just starting to learn Python, and encounter a problem Gordon Levi <gordon@address.invalid> - 2016-07-23 00:37 +1000
Re: Just starting to learn Python, and encounter a problem justin walters <walters.justin01@gmail.com> - 2016-07-22 08:04 -0700
Re: Just starting to learn Python, and encounter a problem MRAB <python@mrabarnett.plus.com> - 2016-07-22 16:08 +0100
csiph-web