[Tutor] 2 basic problems
John Harris
john at johnharris.tv
Sun Jun 16 17:57:00 CEST 2013
Hi Charles,
I have added some comments below explaining why you are not getting the
results you expect:
>>> 4+4
8
The above is an expression, when you hit enter python will evaluate it and give
the answer.
>>> 3+3=4
SyntaxError: can't assign to operator
>>> 3=1
SyntaxError: can't assign to literal
>
I thought the last 2 lines should return False
This is an assignment statement, the single '=' means when you hit enter
python will try to set the value of one side to the other. Hence the
assignment error. Instead try:
>>> 3+3==4
False
>>> 3==1
False
--------
lot=('1'+'6'+'8')
print(lot)
a=open("acc","w")
a.write(lot)
a.close
b=open("acc","r")
b.read()
print (b)
b.close
returns
>>>
168
<open file 'acc', mode 'r' at 0xb6f9a650>
>>>
Firstly your <file>.close methods need to have parens to actually call the
method. So instead of a.close we need a.close()
Then your b.read() line will work, I get the output '168' when I call
b.read().
Lastly, the line b = open("acc", "r") creates a new file object (b). From
the docs: "Open a file, returning an object of the file type."
Therefore when you print(b) python will call the __repr__() method of the
file object which is why you see the line <open file 'acc', mode 'r' at
0xb6f9a650>.
John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130616/34b0e784/attachment.html>
More information about the Tutor
mailing list