-3

I have a simple question here. I have two numbers in a txt file and I am trying to create a method that can change two variables in my code for those two numbers in the txt file.

Here is my method:

def loadCoords(cordX, cordY):
    i=0;
    f1 = open( 'continue.txt', "r")
    f2 = open( 'continue.txt', "r")
    f1.readline();
    while i<2:
        f2.readline();
        i=i+1;
    #already tested>>> cord=f.readline()   xD
    #also tried to put another names for the cordX and cordY here inside  xD
    cordY=f2;
    cordX=f1;
    return cordX;
    return cordY;

Here is where I am calling the method:

if evento.type == pygame.KEYDOWN:
            if evento.key == pygame.K_a:
                   print('GAME BEGIN')
                   GAME_BEGIN = True
                   loadCoords(cordX,cordY);

Someone could help?

angussidney
  • 686
  • 2
  • 13
  • 24
4ury0n
  • 105
  • 1
  • 2
  • 13
  • What's the error you are getting? – sameera sy Nov 07 '16 at 07:19
  • no one, it just can't read the f1 and f2 files, coordX and Y printing 0 and 0 , the numbers that i've been declared at the beggining. – 4ury0n Nov 07 '16 at 07:21
  • Possible duplicate of [How do I read a text file into a string variable in Python](http://stackoverflow.com/questions/8369219/how-do-i-read-a-text-file-into-a-string-variable-in-python) – DeepSpace Nov 07 '16 at 07:22

3 Answers3

0

The method f1.readline() returns something, but you aren't using the returned result (which would be as list in this case). Put the functions output into a variable (like this f1_content = f1.readline()).

YpsilonZett
  • 100
  • 1
  • 7
0

If your text file contains two numbers in separate line then use this

def loadCoords():
    f1 = open( 'continue.txt', "r")
    text=f1.read()
    num_list=text.split(sep='\n')

    cordY=int(num_list[0])
    cordX=int(num_list[1])
    f1.close()
    return cordX,cordY

Don't use two returns in your Function , because the first return will exit from the function , and you lose whatever you return next.

If you are loading two co-ordinates from a text file then why you are passing two arguments to you function, instead load the return value.

cordX,cordY=loadCoords()
R__raki__
  • 847
  • 4
  • 15
  • 30
  • tks for your help and hint @Rakesh_K , i just tried to change my script to your and i have the error : _cordX=int(num_list[0]) ValueError: invalid literal for int() with base 10: '52 150'_ – 4ury0n Nov 07 '16 at 20:12
  • i've never had a problem like this before, maybe could be those '' that the console are reading(but i don't have this in my file)... what do you think it is?? tks – 4ury0n Nov 07 '16 at 20:14
  • forget, it is done, hahaha 'def loadCoords(): f1 = open( "continue.txt", "r") text=f1.read() num_list=text.split() print (num_list) cordX=num_list[0] print(cordX) cordY=num_list[1] f1.close() return cordX,cordY' – 4ury0n Nov 07 '16 at 21:01
  • @Aury0n if the problem is resolved you can accept the answer – R__raki__ Nov 08 '16 at 06:52
0

forget, it is done, hahaha def loadCoords(): f1 = open( "continue.txt", "r") text=f1.read() num_list=text.split() print (num_list) cordX=num_list[0] print(cordX) cordY=num_list[1] f1.close()
return cordX,cordY

4ury0n
  • 105
  • 1
  • 2
  • 13