Change Values of A Variable

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2188 Views - Last Post: 14 February 2011 - 08:08 AM Rate Topic: -----

#1 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Change Values of A Variable

Posted 09 February 2011 - 09:27 PM

Just messing around in python and i want to reassign a value. here is my code
s = ("Sorry Try Again")
name = input("Username:")
if name == ('Nathan'):
    name = True
    password = int(input('Password:'))
    if password == (23):
        password = True
        print ('Welcome')
    elif password != (23):
        password = False
elif name != ('Nathan'):
    name = False
while name == True and password ==True:
        print('\n\t\t\tOptions\n')
        change = int(input('1 Change Passsword \n2 End \n\n'))
        if change == 1:
            newpassword = input('New password: ')
        elif change == 2:
            print('ending')
            break

while name == False or password == False:
    print(s)
    break


The program goes like this. It asks for a Username and password and if both are correct, it displays options. I have a change password option and i want it to change the value of the original password to the new one inputed by the user. What would be the best method for doing so?

Is This A Good Question/Topic? 0
  • +

Replies To: Change Values of A Variable

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Change Values of A Variable

Posted 09 February 2011 - 09:34 PM

Instead of hard coding the values like you do here:
if name == ('Nathan'): // here
    name = True
    password = int(input('Password:'))
    if password == (23): // here



Make a variable called username and a variable called password. That way, you can compare, but change if needed.

username = 'Nathan'
password = '23'

s = ("Sorry Try Again")
name = input("Username:")
if name == username:
    name = True
    password = input('Password:')
    if password == password:
        password = True
        print ('Welcome')
    elif password != (23):
        password = False
elif name != ('Nathan'):
    name = False
while name == True and password ==True:
        print('\n\t\t\tOptions\n')
        change = int(input('1 Change Passsword \n2 End \n\n'))
        if change == 1:
            newpassword = input('New password: ')
        elif change == 2:
            print('ending')
            break

while name == False or password == False:
    print(s)
    break



Now, if you need to change the values, you can change username and password.
Was This Post Helpful? 1
  • +
  • -

#3 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: Change Values of A Variable

Posted 09 February 2011 - 09:43 PM

Will that permanently store the new value?

This post has been edited by JackOfAllTrades: 12 February 2011 - 11:49 AM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: Change Values of A Variable

Posted 09 February 2011 - 09:45 PM

No. Whenever the program is reset, the username and pass will return to their state. I thought you meant you needed to change DURING the run. To make it permanent, you can hardcode the values, have the Python program read from a file which contains the user and pass. You could use a database, but that seems overkill for this problem.
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Change Values of A Variable

Posted 10 February 2011 - 08:41 AM

In order to permanently change the password, you'll need to change the structure of your program a little. Your name and password will need to be saved to a file (some people would use a database, but that's overkill for this program), when your program starts you'll need to read the name and password from that file, and your program will need to update the file accordingly when those credentials change.

This post has been edited by atraub: 11 February 2011 - 07:26 AM

Was This Post Helpful? 1
  • +
  • -

#6 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: Change Values of A Variable

Posted 11 February 2011 - 07:39 AM

View Postatraub, on 10 February 2011 - 08:41 AM, said:

In order to permanently change the password, you'll need to change the structure of your program a little. Your name and password will need to be saved to a file (some people would use a database, but that's overkill for this program), when your program starts you'll need to read the name and password from that file, and your program will need to update the file accordingly when those credentials change.

I Think I have input output now, but I have structured something wrong. After I enter the correct username, it displays the line i have to tell me its wrong.
 fin = open('info.dat', 'r')
username = fin.readline()
password = fin.readline()
fin.close() 

s = ("Sorry Try Again")
name = input("Username:")
if name == username:
    name = True
    password = input('Password:')
    if password == password:
        password = True
        print ('Welcome')
    elif password != (23):
        password = False
elif name != ('Nathan'):
    name = False
while name == True and password ==True:
        print('\n\t\t\tOptions\n')
        options = int(input('1 Change Passsword \n2 End \n\n'))
        if options == 1:
            fout = open('info.dat', 'w')
            fout.write("input('New password: ')")
            fout.close()
        elif options == 2:
            print('ending')
            break

while name == False or password == False:
    print(s)
    break



Output:
 Username:nathan
Sorry Try Again
>>> 


here are the contents of my data file
username = nathan
password = 23


Was This Post Helpful? 0
  • +
  • -

#7 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Change Values of A Variable

Posted 11 February 2011 - 07:41 AM

Very nice, I like your initiative. Change your data file from:
username = nathan
password = 23


To:
nathan
23

Was This Post Helpful? 0
  • +
  • -

#8 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: Change Values of A Variable

Posted 11 February 2011 - 07:47 AM

View Postatraub, on 11 February 2011 - 07:41 AM, said:

Very nice, I like your initiative. Change your data file from:
username = nathan
password = 23


To:
nathan
23

Still the same response. Could it have some thing to do with data types?
Was This Post Helpful? 0
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Change Values of A Variable

Posted 11 February 2011 - 07:48 AM

D'oh! readline() includes the new line character at the end of the line (\n). Change your code accordingly and it should work.

 fin = open('info.dat', 'r')
username = fin.readline()[:-1]#trims the last character
password = fin.readline()[:-1]#trims the last character
fin.close() 

s = ("Sorry Try Again")
name = input("Username:")
if name == username:
    name = True
    password = input('Password:')
    if password == password:
        password = True
        print ('Welcome')
    elif password != (23):
        password = False
elif name != ('Nathan'):
    name = False
while name == True and password ==True:
        print('\n\t\t\tOptions\n')
        options = int(input('1 Change Passsword \n2 End \n\n'))
        if options == 1:
            fout = open('info.dat', 'w')
            fout.write("input('New password: ')")
            fout.close()
        elif options == 2:
            print('ending')
            break

while name == False or password == False:
    print(s)
    break



This post has been edited by atraub: 11 February 2011 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#10 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: Change Values of A Variable

Posted 11 February 2011 - 07:52 AM

This is the output
nathan

23

Username:nathan
Sorry Try Again

Was This Post Helpful? 0
  • +
  • -

#11 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Change Values of A Variable

Posted 11 February 2011 - 07:53 AM

see the edit I made to my last post ^_^ That SHOULD fix it my friend. Currently username is actually equal to 'nathan\n'

This post has been edited by atraub: 11 February 2011 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#12 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: Change Values of A Variable

Posted 11 February 2011 - 07:58 AM

View Postatraub, on 11 February 2011 - 07:53 AM, said:

see the edit I made to my last post ^_^ That SHOULD fix it my friend. Currently username is actually equal to 'nathan\n'

It does! you just reposted before i had the chance to check the output. The input is now working, I need to work on the output though. Let me try to work through this....
Was This Post Helpful? 0
  • +
  • -

#13 Python91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 09-February 11

Re: Change Values of A Variable

Posted 11 February 2011 - 08:17 AM

Success!!! I have input/output that works. Thanks for the help.now Just to find something useful to do with it.
fin = open('info.dat', 'r')
username = fin.readline()[:-1]
password = fin.readline()[:-1]
fin.close()

print(username)
print(password)

s = ("Sorry Try Again")
name = input("Username:")
if name == username:
    name = True
    password = input('Password:')
    if password == password:
        password = True
        print ('Welcome')
    elif password != (23):
        password = False
elif name != ('Nathan'):
    name = False
while name == True and password ==True:
        print('\n\t\t\tOptions\n')
        options = int(input('1 Change Passsword \n2 End \n\n'))
        if options == 1:
            fout = open('info.dat', 'w')
            x = input('New password\n')
            fout.write(username)
            fout.write('\n')
            fout.write(x)
            fout.close()
        elif options == 2:
            print('ending')
            break

while name == False or password == False:
    print(s)
    break

Was This Post Helpful? 0
  • +
  • -

#14 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Change Values of A Variable

Posted 11 February 2011 - 08:20 AM

This program probably isn't going to have much utility I'm afraid. However, you now have a basic foundation for File I/O (input/output), which will be immensely beneficial in your programming ventures. Congratulations Python91, this is a topic that many programming courses don't cover until closer to the end of the course. I do see one thing you should change though. the password you read from a file is being called 'password' and the password a user enters is called 'password'. That is a problem because you're comparing the password the user enters to itself. A quick fix would be:

fin = open('info.dat', 'r')
savedUsername = fin.readline()[:-1]
savedPassword = fin.readline()[:-1]
fin.close()

s = ("Sorry Try Again")
username = input("Username:")
if username == savedUsername:
    name = True
    password = input('Password:')
    if password ==savedPassword:
        password = True
        print ('Welcome')
    else: #if the password isn't right
        password = False
else:#if the name isn't right
    name = False
while name == True and password ==True:
        print('\n\t\t\tOptions\n')
        options = int(input('1 Change Passsword \n2 End \n\n'))
        if options == 1:
            fout = open('info.dat', 'w')
            x = input('New password\n')
            fout.write(username)
            fout.write('\n')
            fout.write(x)
            fout.close()
        elif options == 2:
            print('ending')
            break

while name == False or password == False:
    print(s)
    break


This post has been edited by atraub: 11 February 2011 - 08:25 AM

Was This Post Helpful? 1
  • +
  • -

#15 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Change Values of A Variable

Posted 11 February 2011 - 08:47 AM

Looking at your program, there are a few other ways it can be cleaned up bit. First and foremost, it's always a great idea to use functions. It's an easy way to break your program into smaller, more managable pieces. I hope you don't mind that I took the liberty to edit it:

def main():
    savedUsername, savedPassword = getUsernameAndPassword()
    
    while True:
        if infoIsValid(savedUsername, savedPassword):
            print ('Welcome')
            break
        else:
            print("Sorry, try again\n\n")

    while True:
       print('\n\t\t\tOptions\n')

       options = int(input('1 Change Passsword \n2 End \n\n'))
       if options == 1:
           changePassword(savedUsername)

       elif options == 2:
           print('ending')
           return 0

def getUsernameAndPassword(filename='info.dat'):
    fin = open(filename, 'r')
    name = fin.readline()[:-1]
    password = fin.readline()[:-1]
    fin.close()
    return name,password

def infoIsValid(savedUsername,savedPassword):
    username = input("Username:")
    password = input("Password:")
    return username == savedUsername and password == savedPassword
    
def changePassword(username, filename='info.dat'):
    fout = open(filename, 'w')
    x = input('New password\n')
    fout.write(username+'\n')
    fout.write(x+'\n')
    fout.close()


if __name__ == "__main__":
    main()



Don't worry too much about the if __name__ == "__main__" it's just a convention that tells your program how to start if it's the main part of your program. Everything else should be pretty straight forward. What do you think about these modifications? It's still your code, just a little more organized.

This post has been edited by atraub: 11 February 2011 - 11:53 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2