file handling issues
Leo Carnovale
leo.carnovale at gmail.com
Fri Sep 6 23:05:15 EDT 2013
On Saturday, 7 September 2013 13:03:14 UTC+10, Leo Carnovale wrote:
> On Saturday, 7 September 2013 02:17:03 UTC+10, Piet van Oostrum wrote:
>
> > 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]
>
>
>
> Yes the code is a mess, I have been tempted to re write the whole thing...
>
> The reason for the messiness is firstly because I am relatively new to programming and because this is the result of me tinkering around with previous code, which was still messy, but cleaner than this.
>
>
>
> So the open("key","w") clears the file straight away? That helps a lot, thanks, this will definitely save me a lot of time in the future. Also the user doesn't guess the pass, I would give it to them. After they enter it, they go on to the guessing game (not shown in the code).
>
>
>
> Thanks for the help!
>
>
>
> Leo
Ah and one other thing!
What is this crypto algorithm you speak of? I desperately need some sort of encryption as at the moment anyone can simply open the text file and change the numbers to numbers that work!
Where can I learn more about it?
More information about the Python-list
mailing list