hello I'm trying to create program to check for the user password"only numbers" if it's correct then go to the next step. if not just type Invalid password
the password should be store in another file which is "password.txt"

this is my code but I couldn't make the program check the password from password.txt

import pickle
f= open ("pin.txt", "w")
pickle.dump(1234, f)
pickle.dump([1,2,3,4], f)
f.close()

def passw():
   f=open("password.txt", "r")
   x=pickle.load(f)
   pin = raw_input ("Please insert your password number: ")
   while True:
      if pin >= 1000:
         print "password accepted"
      if pin <= 9999:
         print "password accepted"
         print
      if pin == x:
         print "Valid password"
         print
      if pin != x:
         print "Invalid password"
         print
      else:
         print "Invalid password"
passw()

thank you in advance

Recommended Answers

All 22 Replies

You are comparing string from user with binary data. Print values before condition to check what you are comparing. Pin.txt is never used. You should use normal text file.

thank you pyTony

but still have the same problem when I change it to password.txt. I actually I didn't understand how to put values before condition to check what you are comparing.

import pickle
f= open ("password.txt", "w")
pickle.dump(1234, f)
pickle.dump([1,2,3,4], f)
f.close()
 
def passw():
   f=open("password.txt", "r")
   x=pickle.load(f)
   pin = raw_input ("Please insert your password number: ")
   while True:
      if pin >= 1000:
         print "password accepted"
      if pin <= 9999:
         print "password accepted"
         print
      if pin == x:
         print "Valid password"
         print
      if pin != x:
         print "Invalid password"
         print
      else:
         print "Invalid password"
passw()

print(x,type(x), pin, type(pin))

thank you, I'll try it

import pickle
f= open ("password.txt", "w")
pickle.dump(1234, f)
pickle.dump([1,2,3,4], f)
f.close()

def passw():
   f=open("password.txt", "r")
   x=pickle.load(f)
   pin = raw_input ("Please insert your password number: ")
   while True:
      print(x,type(x), pin, type(pin))
      if pin >= 1000:
         print "Pin accepted"
      if pin <= 9999:
         print "Pin accepted"
         print
      if pin == x:
         print "Valid Pin"
         print
      if pin != x:
         print "Invalid Pin"
         print
      else:
         print "Invalid Pin"
passw()

I got infinite loop when I use this code, I don't know what's wrong

Without a 'break' or 'return' statement, a 'while True' loop is always infinite (unless one of its lines raises an exception, which is not the case here).
Also there is a hidden error, try to understand this:

>>> "1000" < 9999
False

ok I change it into

import pickle
f= open ("password.txt", "w")
pickle.dump(1234, f)
f.close()

def passw():
   f=open("password.txt", "r")
   x=pickle.load(f)
   pin = raw_input ("Please insert your password number: ")
   while True:
      print(x,type(x), pin, type(pin))
      if pin == x:
         print "Valid Pin"
         break
      if pin != x:
         print "Invalid Pin"
         break
      else:
         print "Invalid Pin"
         break
   return pin

passw()

but still the program doesn't check the pin from the file

Are the types same?

yes sir, I type it as 1234 but doesn't work

str1 = \
"""1234"""
 
a = "password.txt"
fout = open(a, "w")
fout.write(str1)
fout.close()
 
fin = open(a, "r")
str2 = fin.read()
print str2

def passw():
   f=open("password.txt", "r")
   str2 = fin.read()
   pin = raw_input ("Please insert your password number: ")
   while True:
      print(str2,type(str2), pin, type(pin))
      if pin == str2:
         print "Valid Pin"
         break
      if pin != str2:
         print "Invalid Pin"
         break
      else:
         print "Invalid Pin"
         break
   return pin

passw()

I try this code and I got

Please insert your password number: 1234
('', <type 'str'>, '1234', <type 'str'>)
Invalid Pin
'1234'

You called fin.read() twice. It doesn't work because the file position is at the end of the file for the second read, so the empty string is read.

Also your while loop is never executed more than once.

Gribouillis & pyTony thank you too
I found my mistakes

guys

how if I want to expand my program to ask about user name....

if the pin vaild goes to another function such as def usname: , and if not valid return false ???

str1 = \
"""1234"""
 
a = "password.txt"
fout = open(a, "w")
fout.write(str1)
fout.close()
 
fin = open(a, "r")
str2 = fin.read()
print str2

def passw():
   f=open("password.txt", "r")
   str2 = fin.read()
   pin = raw_input ("Please insert your password number: ")
   while True:
      print(str2,type(str2), pin, type(pin))
      if pin == str2:
         print "Valid Pin"
         break
      if pin != str2:
         print "Invalid Pin"
         break
      else:
         print "Invalid Pin"
         break
   return pin

def usname():
   a=open("usname.txt", "r")
   user= raw_input ("please enter your user name:")
   while True:
      if user == str3:
         print "Valid username"
         break
      if pin != str3:
         print "Invalid username"
         break
      else:
         print "Invalid username"
         break
   return user

I don't know how to make connection between these two functions which means only if the first one is valid pin go to the other function

any help regarding to if I want to expand my program to ask about user name....

if the pin vaild goes to another function such as def usname: , and if not valid return false ???

Instead of the 'break' statements in the invalid cases, you could use 'return None'. This would allow you to write code like

password = passw()
if password is not None: # password was ok
    user = usname()
    if user is not None: # user was ok
        print "success"

However, programs usually ask simultaneously the user name and the password, and don't say which of the user name or the password failed in case of failure. That would mean more changes to your program.

thank you, but even if the its invalid pin they ask about the user name
what I need just ask about the name only if the pin is correct otherwise exit the program or anything.

def passw():
   f=open("password.txt", "r")
   pin = raw_input ("Please insert your password number: ")
   while True:
      if pin == str2:
         print "Valid Pin"
         return True
      if pin != str2:
         print "Invalid Pin"
         return None
      else:
         print "Invalid Pin"
         return None
   
   password = passw()
   if password is not None: # password was ok
      user = usname()
      if user is not None: # user was ok

def usname():
   a=open("usname.txt", "r")
   user= raw_input ("please enter your user name:")
   while True:
      if user == str3:
         print "Valid username"
         break
      if pin != str3:
         print "Invalid username"
         break
      else:
         print "Invalid username"
         break
   return user
         print "success"

it could be much shorter:

#User and Pass files:

userN = open('username.txt', 'r').read()
pasN = open('password.txt', 'r').read()

# First make your Functions and Then Use Them the way you want:

def username():
    pin = raw_input ("Please insert your username: ")
    while True:
        if pin == userN:
            print 'You Did It!'
            break
        else:
            print 'Wrong Username!!!'
            return

def password():
    pin = raw_input ("Please insert your password number: ")
    while True:
        if pin == pasN:
            print 'Correct!\nNow Enter Username'
            username()
            break
        else:
            print 'Wrong Password!!!'
            return

password()

thank you M.S, but what I need is different

I need only if the pin code correct ask about the username"as second level" and if the pin code wrong don't ask about anything else.

thank you all SOLVED

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.