Just starting to learn Python, and encounter a problem
Random832
random832 at fastmail.com
Fri Jul 22 10:30:05 EDT 2016
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")
More information about the Python-list
mailing list