Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111758
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Just starting to learn Python, and encounter a problem |
| Date | 2016-07-22 16:08 +0100 |
| Message-ID | <mailman.54.1469200100.22221.python-list@python.org> (permalink) |
| References | <ac35006b-ab2e-44f1-b896-cd57d9689c73@googlegroups.com> <685410d4-3db5-68ed-50c3-3a0d2646fbce@mrabarnett.plus.com> |
On 2016-07-22 14:59, Zagyen Leo 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.
>
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!)
Back to comp.lang.python | Previous | Next — Previous 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