interactive programming
Adam DePrince
adam at deprince.net
Fri Feb 23 01:44:50 EST 2001
Chris Richard Adams wrote:
>
> How can I create a loop - interactively. I'm just trying to learn
> syntax for example, so Id like to create conditional if statements like
> a = 'feb'
>
> if a == 'jan':
> mo = 1
> elif a == 'feb':
> mo = 2
> elif a == 'mar':
> mo = 3
> ...
>
> I tried to type this interactively, but I get a mess of error about
> syntax or mis placed whitespace...
>
> 1.) After the return after mo = 1 - how do I make sure theelif is
> aligned with if?
>
> 2.) Is there any refernces for interactive programming - the tutorial is
> too short?
>
> Thanks,Chris
Ah. To end a block interactively you must hit enter twice.
$ python
Python 2.0 (#1, Dec 18 2000, 00:59:58)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> for x in range(10):
... print x <- Enter
... <- Enter
0
1
2
3
4
5
6
7
8
9
>>>
Your code:
> if a == 'jan':
> mo = 1
> elif a == 'feb':
> mo = 2
> elif a == 'mar':
> mo = 3
> ...
>
would require an extra enter after mo=12.
Instead of using nested ifs, I would do this:
months = {"jan":1,"feb":2,"mar":3 ... :12}
b = months[a]
Of course, you don't want to create months each and every time you do
the lookup, otherwise you havn't really gained anything.
Adam DePrince
Starmedia Network, Inc.
Email:
zlib.decompress('x\332KLI\314\325KI-(\312\314KNu(.I,\312MM\311L\324K\316\317\005\000\221\331\012s')
More information about the Python-list
mailing list