file handling issues

Piet van Oostrum piet at vanoostrum.org
Fri Sep 6 12:17:03 EDT 2013


leo.carnovale at gmail.com writes:

> I am making this little game and I am trying to make some sort of script that does the following:
>>Checks to see if a file exists
>  >If it does, check the numbers in it
>  >If it doesn't, make one and fill it with some numbers
>>Sorts out if the numbers in the file are in the right format
>  >If they are, then store them
>  >If not, remake it again
>>THEN, check if there is a pass.
>  >If there is, check if it fits the key and go on if so
>  >If not, ask for a new one, check it, go on if its correct.
>
> Every time I run it, it says the key is 0!?
> I can not for the life of me figure out what it is doing.
>
> The code:
> <https://docs.google.com/document/d/1_Qo4bgmNhRer3fAt3EAtPNSei4N2q58f_q4_9r6OHbw/edit?usp=sharing>
>

What a mess is that code. First that messing around with the key is
crazy. You probably want to do some crypto on it, but why don't you just
use some standard crypto algorithm?

The you have defined T as True and F as False, but sometimes you use T
and sometimes True, and as far as I can see you never use F. Just stay
with True and false as it makes the code more readable.

Once you use "with open('Key')", all other case you use
f = open('Key',...)
f.read() or f.write()
f.close()

Be consistenst and use always "with open..."

There are at least three palces where you write a key to the file. Make
a function for this; it makes your code more structured.

And please, put spaces aroud the = signs.

Now the real problem:

    if newpas:
        f=open("Key","w")
        print("No pass found!")
        print("Your wonderful key is: ",int(keys[0]))
        pasw=input("What is your pass?   : ")
    elif newkey:
        f=open("Key","w")

Here you open the file for writing but you never write anything to it.
This makes the file empty. And apparently you do not check this the next
time you open it. If you would have used a function to write a key to
the file, this probably would not have occurred.

    if mess_with(keys[0])==pasw:
        hesin=1
        print("Your in!")
        f=open("Key","w")

Here you open the file again with the same variable. This causes the old
value to be lost and the file probably to be closed. Just messy.

        print("writing %s" % str(keys[0])+pasw)
        f.write(str(keys[0])+pasw)
        f.close()
    else:
        hesin=0

And how are people supposed to guess an 8 character password? Are they
supposed to do that weird calculation on their calculators or some such?

-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list