Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111750 > unrolled thread
| Started by | Zagyen Leo <zagyen@gmail.com> |
|---|---|
| First post | 2016-07-22 06:59 -0700 |
| Last post | 2016-07-22 16:08 +0100 |
| Articles | 10 — 8 participants |
Back to article view | Back to comp.lang.python
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
| From | Zagyen Leo <zagyen@gmail.com> |
|---|---|
| Date | 2016-07-22 06:59 -0700 |
| Subject | Just starting to learn Python, and encounter a problem |
| Message-ID | <ac35006b-ab2e-44f1-b896-cd57d9689c73@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Random832 <random832@fastmail.com> |
|---|---|
| Date | 2016-07-22 10:30 -0400 |
| Message-ID | <mailman.49.1469197808.22221.python-list@python.org> |
| In reply to | #111750 |
On Fri, Jul 22, 2016, at 09: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.
"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")
[toc] | [prev] | [next] | [standalone]
| From | Zagyen Leo <zagyen@gmail.com> |
|---|---|
| Date | 2016-07-22 23:13 -0700 |
| Message-ID | <ccd4e785-6c10-4c13-a59a-a95b4ba89e66@googlegroups.com> |
| In reply to | #111752 |
I got it! Thank you. Hope in one day I could help other newbies as you do.
[toc] | [prev] | [next] | [standalone]
| From | Bob Gailer <bgailer@gmail.com> |
|---|---|
| Date | 2016-07-22 10:36 -0400 |
| Message-ID | <mailman.50.1469198194.22221.python-list@python.org> |
| In reply to | #111750 |
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"):
[toc] | [prev] | [next] | [standalone]
| From | gst <g.starck@gmail.com> |
|---|---|
| Date | 2016-07-23 11:18 -0700 |
| Message-ID | <e6f7d426-bbb8-47d8-9326-2d34d4ed4737@googlegroups.com> |
| In reply to | #111753 |
Heuh case 2 : "String1" or "String2" Evaluates to "String1" ?
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-07-23 20:06 +0100 |
| Message-ID | <mailman.86.1469300796.22221.python-list@python.org> |
| In reply to | #111796 |
On 2016-07-23 19:18, gst wrote:
> Heuh case 2 :
>
> "String1" or "String2"
>
> Evaluates to "String1" ?
>
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'.)
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-07-24 11:58 +1000 |
| Message-ID | <579420dc$0$22141$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #111796 |
On Sun, 24 Jul 2016 04:18 am, gst wrote: > Heuh case 2 : > > "String1" or "String2" > > Evaluates to "String1" ? 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.
[toc] | [prev] | [next] | [standalone]
| From | Gordon Levi <gordon@address.invalid> |
|---|---|
| Date | 2016-07-23 00:37 +1000 |
| Message-ID | <iva4pb9nc50pa8tdca5mak6fnt3fvj5lc1@4ax.com> |
| In reply to | #111750 |
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.
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":
[toc] | [prev] | [next] | [standalone]
| From | justin walters <walters.justin01@gmail.com> |
|---|---|
| Date | 2016-07-22 08:04 -0700 |
| Message-ID | <mailman.53.1469199887.22221.python-list@python.org> |
| In reply to | #111754 |
:
On Jul 22, 2016 7:46 AM, "Gordon Levi" <gordon@address.invalid> wrote:
>
> 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.
>
> 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":
> --
> 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')
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2016-07-22 16:08 +0100 |
| Message-ID | <mailman.54.1469200100.22221.python-list@python.org> |
| In reply to | #111750 |
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!)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web