while的loop

非常基本,但这是很值得我记住的一课。问题其实很简单,只是脑子没有转过来。而且我不确定的是到底是一时没捅破窗户纸,还是一直没有这种sense。后者就比较糟糕了——但我记得我如今做的很多事都是为了打破这种成见。

照书撸了段程序却不知道怎么执行的。按照自己的理解把程序改了改,居然效果一样。郁闷的是还是不知道怎么执行的。

啊羊和啊黄都提供了非常耐心的指导。啊羊让我把代码放到gist里去,告诉我赋值的概念,啊黄则告诉我python是一条一条执行程序的。两人都不约而同地在微信里说:头脑模拟执行。

我当时的回答是,I have no idea what you are talking about.

但想通了就很简单了。真开心。

btw,gist是个好东西。可以放代码片段。

cathysia@mancinism:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> length=5
>>> breadth=2
>>> area=length*breadth
>>> print ‘Area is’, area;print ‘Perimeter is’, 2*(length+breadth)
Area is 10
Perimeter is 14
>>>
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ if.py
if.py: command not found
cathysia@mancinism:~$ python if.py
File “if.py”, line 3
if guess == number
^
SyntaxError: invalid syntax
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python if.py
File “if.py”, line 5
print ‘Congratulations, you guessed it.’
^
IndentationError: expected an indented block
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python if.py
File “if.py”, line 8
elif guess < number
^
SyntaxError: invalid syntax
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python if.py
Enter an integer:40
No, it is a litle higher than that
cathysia@mancinism:~$ 50
50: command not found
cathysia@mancinism:~$ python if.py
Enter an integer:60
No, it is a little lower than that
Done
cathysia@mancinism:~$ python if.py
Enter an integer:42
Congratulations, you guessed it.
(but you do not win any prizes!)
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python if.py
Enter an integer:42
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
cathysia@mancinism:~$ python if.py24
python: can’t open file ‘if.py24’: [Errno 2] No such file or directory
cathysia@mancinism:~$ python if.py
Enter an integer:24
No, it is a litle higher than that
Done
cathysia@mancinism:~$ python if.py
Enter an integer:43
No, it is a little lower than that
Done
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python while.py
Enter an integer : 23
No, it is a little higher than that.
Enter an integer : 295
The while loop is over.
Enter an integer : 42
Congratulations, you guessed it.
Traceback (most recent call last):
File “while.py”, line 8, in <module>
running=faulse
NameError: name ‘faulse’ is not defined
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ while.py
while.py: command not found
cathysia@mancinism:~$ python while.py
Enter an integer : 42
Congratulations, you guessed it.
Done
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python while.py
Enter an integer : 24
No, it is a little higher than that.
Enter an integer : 43
The while loop is over.
Enter an integer : 42
Congratulations, you guessed it.
Done
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python while.py
Enter an integer : 24
No, it is a little higher than that.
Enter an integer : 43
Enter an integer : 42
Congratulations, you guessed it.
The while loop is over.
Done
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python while.py
Enter an integer : 24
No, it is a little higher than that.
Enter an integer : 43
Enter an integer : 42
Congratulations, you guessed it.
The while loop is over.
Done
cathysia@mancinism:~$ vim
cathysia@mancinism:~$ python while.py
Enter an integer : 24
No, it is a little higher than that.
Enter an integer : 43
No, it is a alittle lower thant that.
Enter an integer : 42
Congratulations, you guessed it.
The while loop is over.
Done
cathysia@mancinism:~$ vim
cathysia@mancinism:~$

NOte 03oct15

cathysia@mancinism:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> print “What’s your name?”
What’s your name?
>>> print ‘What\’s your name’
What’s your name
>>> print “\\”What’s your name\\” I asked.”
File “<stdin>”, line 1
print “\\”What’s your name\\” I asked.”
^
SyntaxError: invalid syntax
>>> print ‘What\”s your name’
What”s your name
>>> print ‘This is the first line
File “<stdin>”, line 1
print ‘This is the first line
^
SyntaxError: EOL while scanning string literal
>>> print ‘This is the first line\nThis is the second line’
This is the first line
This is the second line
>>> Print ‘This is the first line. \
… This is the second line.’
File “<stdin>”, line 2
This is the second line.’
^
SyntaxError: invalid syntax
>>> Print ‘This is the first line. \ This is the second line.’
File “<stdin>”, line 1
Print ‘This is the first line. \ This is the second line.’
^
SyntaxError: invalid syntax
>>> print “THis is the first line. \ This is the second line.”
THis is the first line. \ This is the second line.
>>> print “This is the first line.\This is the second line.”
This is the first line.\This is the second line.
>>> r”Newlines are indicated by \n”
‘Newlines are indicated by \\n’
>>> print r”Newlines are indicated by\n”
Newlines are indicated by\n
>>>