Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
odd or even minute
#11
(Dec-28-2019, 05:07 PM)Dixon Wrote: When I entered 'odds' on one line it worked. I guess the book is in error where is says on page 13 that it's OK to enter the 'odds' numbers on several lines.

No guessing here, this error is yours.

From the book:

Quote:Be sure to enter the code exactly as shown here
/.../
Note how the odds list extends over three lines, despite being a single statement. This is OK, as the interpreter won’t decide a single statement has come to an end until it finds the closing bracket (]) that matches the opening one ([). Typically, the end of the line marks the end of a statement in Python, but there can be exceptions to this general rule, and multiline lists are just one of them (we’ll meet the others later).

You didn’t entered code exactly. You didn’t follow opening and closing brackets.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#12
(Dec-28-2019, 09:22 PM)perfringo Wrote:
(Dec-28-2019, 05:07 PM)Dixon Wrote: When I entered 'odds' on one line it worked. I guess the book is in error where is says on page 13 that it's OK to enter the 'odds' numbers on several lines.

No guessing here, this error is yours.

From the book:

Quote:Be sure to enter the code exactly as shown here
/.../
Note how the odds list extends over three lines, despite being a single statement. This is OK, as the interpreter won’t decide a single statement has come to an end until it finds the closing bracket (]) that matches the opening one ([). Typically, the end of the line marks the end of a statement in Python, but there can be exceptions to this general rule, and multiline lists are just one of them (we’ll meet the others later).

You didn’t entered code exactly. You didn’t follow opening and closing brackets.

You're right. A beginner's error. I'll make amends.
Reply
#13
With the help of nilamo, came up with:
print(["Even" if z % 2 == 0 else "Odd" for z in [num] ])
Modified Jan 2 to function:
>>> def odd_or_even(num):
...     return ["Even" if z % 2 == 0 else "Odd" for z in [num] ][0]
... 
>>> print(odd_or_even(42))
Even
>>> print(odd_or_even(43))
Odd
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Having trouble with minute stock data MAZambelli4353 2 2,334 Sep-03-2019, 09:41 AM
Last Post: perfringo
  python crontab remove_all(comment="Minute*") vvarrior 1 2,770 Aug-06-2018, 12:39 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020