#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Update/ Change a value in a function


    Hi, Designing an ATM machine program for college work.

    I have this function, which requires the user to enter their 4-digit pin number. (Code below)

    I then have an option in the menu called ''Change PIN''.

    I've tried using global pin, but not sure if I am using it correctly.
    Total newbie to programming.

    I have quit a large program and this is the only thing that i just can't seem to solve. Working on it for hours now and I just can't get it but I feel I'm close.

    Hopefully when I gain some more knowledge I can give back to these forums cause they've helped me in the past.

    Take care guys, thank you v much.


    At the moment i just have the pin of this account as 9999 and the program will break after 3 incorrect attempts.

    PIN Function:

    def validatePIN():
    global pin
    pin=input("Please enter your 4-digit PIN: ")
    count=1

    while (pin!="9999") and (count<3):
    pin=input("Please enter your 4-digit PIN: ")
    count=count+1


    if(pin!="9999"):
    print("Invalid PIN - Account Blocked")
    return False
    else:
    return True



    Then further down, I have a function to change the PIN but I'm just confused about it and it's not valid at all.

    Current Change PIN function:

    def CP():
    currentPin=input("Please enter your current PIN:")

    if (currentPin != "9999"):
    print("That is not your current PIN")
    currentPin=input("Please enter your current PIN:")

    elif (currentPin == "9999"):
    newPin=input("Please enter your new PIN: ")
    print("Thank you. Your new PIN is: ",newPin)
    return newPin

    The problem with this is that it is not global and if I choose to display the pin, it still says 9999.

    I would like this new pin to go in place of the old pin.

    Sorry I'm just very confused!
    Any help is truly appreciated.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    Do not "hard code" a specific pin. Pass the current pin as a function argument. Reuse your validatePIN function. Avoid global if reasonable. Read and follow the python posting instructions linked at my signature. Most of all, thank you for showing an effort.
    Code:
    # python3
    
    def validatePIN(pin):
        attempt=input("Please enter your 4-digit PIN: ")
        count=1
        while (attempt != pin) and (count<3):
            attempt=input("Please enter your 4-digit PIN: ")
            count+=1
        if (attempt == pin):
            return True
        print("Invalid PIN - Account Blocked")
        return False
    
    def CP(pin):
        if validatePIN(pin):
            pin=input("Please enter your new PIN: ")
            print("Thank you. Your new PIN is: ",pin)
        return pin
    
    pin = CP('9999')
    Last edited by b49P23TIvg; November 26th, 2012 at 12:29 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Do not "hard code" a specific pin. Pass the current pin as a function argument. Reuse your validatePIN function. Avoid global if reasonable. Read and follow the python posting instructions linked at my signature. Most of all, thank you for showing an effort.
    Code:
    # python3
    
    def validatePIN(pin):
        attempt=input("Please enter your 4-digit PIN: ")
        count=1
        while (attempt != pin) and (count<3):
            attempt=input("Please enter your 4-digit PIN: ")
            count+=1
        if (attempt == pin):
            return True
        print("Invalid PIN - Account Blocked")
        return False
    
    def CP(pin):
        if validatePIN(pin):
            pin=input("Please enter your new PIN: ")
            print("Thank you. Your new PIN is: ",pin)
        return pin
    
    pin = CP('9999')
    Thank you very much Sir. Your help is deeply appreciated!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0

    New to Python


    Hi guys I am a total noob at python and was wondering if you could help me with using a pin number on my midterm it is a simple ATM program using Python.

    I used the code for the pin number that was posted here but one thing I would like it to do is actually not let you go any further after the 3 try's.

    At the moment it will print "Invalid PIN - Account Blocked" but if you keep hitting the forward button on the Online Python Visualizer it will just keep going on too the next part.

    Is there a way to make it not go any further and sort of end it there after the 3 try's sort of like break gets you out of a loop?

    Thanks in advance for any help ohh by the way I have only been at studying python for about 4 weeks now "boy" do I have a long way to go.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,892
    Rep Power
    481
    Nice! You stole code for your midterm.
    Well, where the program gives the messages about too many tries you could also call exit.

    exit(0)
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Nice! You stole code for your midterm.
    Well, where the program gives the messages about too many tries you could also call exit.

    exit(0)
    Hahaha yes I did the professor actually kinda encourages to not reinvent the wheel he says that most people take a code that is similar to what they need then tweek it to do what they want. Thats all I am doing hehehe.
    I got to say this the best site ever. And u are the coolest cat here how do I give u rep points? Thx for ur help

IMN logo majestic logo threadwatch logo seochat tools logo