Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

How To End A Programme Naturally

  • 09-11-2016 9:06pm
    #1
    Banned (with Prison Access) Posts: 613 ✭✭✭


    So its me again...yay :o
    Anyways, were now doing for functions in class and the goal is to create a login with password and username that ends the programme if its guessed wrong three times. This hasnt been a problem, the problem has been when I enter the correct one it completes the programme but then calls it from the start again, as in a constant loop


Comments

  • Moderators, Politics Moderators Posts: 41,240 Mod ✭✭✭✭Seth Brundle


    What language?
    Can you describe your steps using pseudocode?


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    kbannon wrote: »
    What language?
    Can you describe your steps using pseudocode?

    Sorry Python
    def welcome_message():
        print ("Welcome To World Bank")
        print ("")
        print ("Please Enter The Following Details")
        print ("")
        print ("Please Be Reminded You Will Be Locked Out After Three Incorrect Attempts")
        print ("")
        print ("----------------------------------")
        print ("")
    def bank_details():
        attempts = 0
        max_attempts = 3
        for attempts in range(3):
            username = input ("Please Enter Your Username: ")
            password = input ("Please Enter Your Password: ")
            if username == "bank_admin" and password == "Hytu76E":
                print ("")
                print ("----------------------------------")
                print ("")
                print ("Your Details Are Correct:")
                print ("")
                print ("Weclome To Your World Bank Account")
                print ("")
                print ("----------------------------------")
                print ("")
                final_message()
            else:
                print ("")
                print ("----------------------------------")
                print ("")
                print ("Incorrect Information Entered Please Try Again")
                print ("")
                print ("----------------------------------")
                print ("")
                attempts += 1
        if attempts == max_attempts:
                print ("You Have Failed To Enter Your Correct Details")
                print ("You Have Been Locked Out And Security Notified")
                print("")
                print ("----------------------------------")
    def final_message():
        print("")
        print("The Programme Has Now Ended And You Have Been Logged Out And Will Need To Log Back In")
        print("")
        print("----------------------------------")
    def main():
        welcome_message()
        bank_details()
    main()
    
    

    So like it runs and all, Im not 100% sure Im wrong tbh. I just feel it should end instead of asking for username etc again. Then again that could exactly be what he wants.
    The thing is, I know I can end it using certain functions but thats not organically and I also have to keep it very easy as its beginners python.
    Its more for my own sanity :pac: incase Ive missed something super obvious


  • Moderators, Politics Moderators Posts: 41,240 Mod ✭✭✭✭Seth Brundle


    Without running it, I'm making a guess here: should this line be indented one more tab...
    if attempts == max_attempts:

    If the user enters the username and password correctly, what stops the code from continuing?


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    kbannon wrote: »
    Without running it, I'm making a guess here: should this line be indented one more tab...
    if attempts == max_attempts:

    If the user enters the username and password correctly, what stops the code from continuing?

    The code does continue. My problem is it starts again when I want the program to end as in complete the full cycle which it does just without starting again afterwards. Like I said Im not sure its actually wrong and thats whats frustrating me :o


  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭PrzemoF


    "For" loop doesn't quit after successful login - that's your problem. Use "while" loop or force exiting "for" loop.
    Also you defined max_attempts - why not use it int the "for" loop?
    max_attempts = 3
        for attempts in range(max_attempt):
    

    I'd use completely different flow, but I don't want to interfere with your process of learning.

    P.S. Draw something like this for your program - you'll see where is the problem.
    ?u=http%3A%2F%2Fwww.codeproject.com%2FKB%2Frecipes%2FPatterns%2Falgorithm.JPG&f=1


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 768 ✭✭✭14ned


    Surely it's
    sys.exit(1)
    
    to exit a Python program immediately?

    Also, you really need to look into triple quoted Python string literals ...

    Niall


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    14ned wrote: »
    Surely it's
    sys.exit(1)
    
    to exit a Python program immediately?

    Also, you really need to look into triple quoted Python string literals ...

    Niall

    Yeah thats not organically though for the right of a proper word :o
    with sys.exit you force the programme to shut down. Plus I was told in my last assignment only stuff in class and the only time that was referenced was when my lecturer told us he didnt want it used :pac:

    Im not sure what you mean by triple quoted python string literals


  • Registered Users, Registered Users 2 Posts: 768 ✭✭✭14ned


    Kal El wrote: »
    Im not sure what you mean by triple quoted python string literals

    Python lets you write raw string literals using triple quotes e.g.
    print("""Hello.
    
    Please enter your password:
    """)
    

    Saves on the many print statements one per line


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    14ned wrote: »
    Python lets you write raw string literals using triple quotes e.g.
    print("""Hello.
    
    Please enter your password:
    """)
    

    Saves on the many print statements one per line

    Ah I googled it and compared with our notes in class and it hasnt come up yet, guess thats for second semester :pac:


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    Would a do/while loop not be better, while not >=3 attempts and password not correct etc etc


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,275 ✭✭✭bpmurray


    I have to admit that I don't know python, but any other programming language will loop if this occurs:
    print("The Programme Has Now Ended And You Have Been Logged Out And Will Need To Log Back In")
        print("")
        print("----------------------------------")
    def main():
        welcome_message()
    
    Basically, you print your goodbye message and drop through into main, starting fromt eh beginning again. Try moving the function main to the top of the file.


  • Registered Users, Registered Users 2 Posts: 8,229 ✭✭✭LeinsterDub


    Kal El wrote: »
    Ah I googled it and compared with our notes in class and it hasnt come up yet, guess thats for second semester :pac:

    What sort of lecturer would discourage independent learning? :confused::confused:


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    bpmurray wrote: »
    I have to admit that I don't know python, but any other programming language will loop if this occurs:
    print("The Programme Has Now Ended And You Have Been Logged Out And Will Need To Log Back In")
        print("")
        print("----------------------------------")
    def main():
        welcome_message()
    
    Basically, you print your goodbye message and drop through into main, starting fromt eh beginning again. Try moving the function main to the top of the file.

    We have to have main at the end. Its one of the criteria for the assignment. He wants it done a certain way


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    What sort of lecturer would discourage independent learning? :confused::confused:

    He doesnt as such discourage independent learning. In fact he tells us to look up stuff and play around with it. Just for our assignments he wants us only use whats been covered in class.


  • Moderators, Politics Moderators Posts: 41,240 Mod ✭✭✭✭Seth Brundle


    Kal El wrote: »
    He doesnt as such discourage independent learning. In fact he tells us to look up stuff and play around with it. Just for our assignments he wants us only use whats been covered in class.
    "Yus can look at other stuff but don't go learning that other stuff"
    Sounds like a crap lecturer!


  • Banned (with Prison Access) Posts: 613 ✭✭✭Kal El


    kbannon wrote: »
    "Yus can look at other stuff but don't go learning that other stuff"
    Sounds like a crap lecturer!

    Why? Were a beginner course surely learning the basics is the way to go then work from that. I think youre misunderstanding what im saying


Advertisement