These are chat archives for ipython/ipython

17th
May 2017
daltorya19
@daltorya19
May 17 2017 09:25
i need some help writing to a file
writing a score to a text file
jvbsl
@jvbsl
May 17 2017 09:26
can't see anything wrong with that
daltorya19
@daltorya19
May 17 2017 09:28
the error is
Traceback (most recent call last):
File "C:\Users\Ryan D\Desktop\Projects\Pieces_of_code\Python\Times table test\Times table test - Copy.py", line 124, in <module>
myfile.write(points)
TypeError: write() argument must be str, not int
jvbsl
@jvbsl
May 17 2017 09:29
myfile.write(str(points))
daltorya19
@daltorya19
May 17 2017 09:35
ok
image.png
what the hell? it wont let me set a var?
what?
jvbsl
@jvbsl
May 17 2017 09:37
you forgot a closing bracket
daltorya19
@daltorya19
May 17 2017 09:38
oh
haha
thanks mate :)
ok @jvbsl so u see how its writing my scores to a txt file right? how do i make it list them instead of writing it sideways?
its writing them like this
010510
jvbsl
@jvbsl
May 17 2017 09:40
append new lines
daltorya19
@daltorya19
May 17 2017 09:40
how do i do that?
im sorry im very new to python
jvbsl
@jvbsl
May 17 2017 09:41
'\n' is the new line character like in pretty much every language
daltorya19
@daltorya19
May 17 2017 09:41
oh thanks
i have no prior programming experience
so how would i incorporate that in?
jvbsl
@jvbsl
May 17 2017 09:42
you can concatenate strings with the + operator
daltorya19
@daltorya19
May 17 2017 09:42
how would i put it into this
with open("your scores.txt", "a") as myfile:
myfile.write(str(points))
jvbsl
@jvbsl
May 17 2017 09:42
str(points) is a string
daltorya19
@daltorya19
May 17 2017 09:42
yep
jvbsl
@jvbsl
May 17 2017 09:42
'\n' is a string
daltorya19
@daltorya19
May 17 2017 09:43
oh
i see
jvbsl
@jvbsl
May 17 2017 09:43
btw. ipython != python
ipython is just a library for python
daltorya19
@daltorya19
May 17 2017 09:43
oh
ok
so i tried, but this doesnt work
lol
with open("your scores.txt", "a") as myfile:
myfile.write(str(\n + points))
jvbsl
@jvbsl
May 17 2017 09:44
str converts an int to strings
and + concatenates strings
it does not concatenate ints and string
daltorya19
@daltorya19
May 17 2017 09:44
yes
jvbsl
@jvbsl
May 17 2017 09:44
\n without the quotations is invalid
daltorya19
@daltorya19
May 17 2017 09:44
oh
jvbsl
@jvbsl
May 17 2017 09:44
'xyz' is a string
daltorya19
@daltorya19
May 17 2017 09:44
yes
jvbsl
@jvbsl
May 17 2017 09:45
so is '\n' and so is 'xyz\n'
daltorya19
@daltorya19
May 17 2017 09:54
@jvbsl now im getting this
Traceback (most recent call last):
File "C:\Users\Ryan D\Desktop\Projects\Pieces_of_code\Python\Times table test\Times table test - Copy.py", line 126, in <module>
myfile.write(str('\n' + points))
TypeError: must be str, not int
Denis Lisov
@tanriol
May 17 2017 09:55
@daltorya19 What are you trying to achieve? Write every score on a new line?
jvbsl
@jvbsl
May 17 2017 09:55
as i said points is int and you can't concatenate an int with a string
daltorya19
@daltorya19
May 17 2017 09:55
yep
i dont know what that means @jvbsl
jvbsl
@jvbsl
May 17 2017 09:55
so str(points)+'\n'
daltorya19
@daltorya19
May 17 2017 09:55
oh
thanks
jvbsl
@jvbsl
May 17 2017 09:56
strings are for text values, while ints are for numbers without decimal points, and numbers are not the same as text
Denis Lisov
@tanriol
May 17 2017 09:57
sc = [1,2,3]
with open('test.txt', 'w') as f:
    for s in sc:
        print(s, file=f)
daltorya19
@daltorya19
May 17 2017 09:57
thanks, this got it working str(points)+'\n')
Min RK
@minrk
May 17 2017 21:03
@ivanov nice!
Paul Ivanov
@ivanov
May 17 2017 22:14
Thanks @minrk - enjoy PyCon!