Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help Requested
#1
So I'm building a miniature code just to simulate a terminal like the command line, how ever it has a log on a phase which requires a password, which is 123 in the example below. I want the command Log off to send it back to the stage where it requires a password but I don't know how. If you could help it would be appreciated.

import time 
import sys


Input = 0
working = 1
Password = 0
Name = "User"
Goodbye = "Goodbye"
Change_Name = 0
Required_Password = "123"
B = "/:"
Change_Password = 0
stage = 0
while working == 1:
    if stage == 0:
        print("Greetings User")
        Password = input("Please enter passcode: ")
        if Password == Required_Password:
            print("Password excepted")
            stage = 1
        else:
            raise SystemExit("Password Check failed")

    C = Name + B

    if stage == 1:
        Input = input(C)

        if Input == "Admin":
            Change_Password = input("Change Password: ")
            if Change_Password == Required_Password:
                print("Sorry, ",Name,", This is the same password")
            else:
                Required_Password = Change_Password
                print("Password changed")               
        if Input == "Show password":
            print(Password)
        if Input == ("Help"):
            print("""
            Admin - Allows you to change the password
            Name - Allows you to change your Name
            Show password - Shows the password
            Help - Shows this screen
            Shut down - Shuts down the system""")
        if Input == ("Shut down"):
            raise SystemExit(Goodbye,Name) 
        if Input == ("Name"):
            Change_Name = input("Change Name: ")
            if Change_Name == Name:
                print("Sorry,",Name,",this is the same name.")
            else:
                Name = Change_Name
                print("Name Changed")
                C = Name + B
        if input == ("Log out"):
            stage = 0
Reply
#2
The easiest way to do this might be to organize the code into a state machine (here's some reading: http://gameprogrammingpatterns.com/state.html).

The basic idea is that your program is always only doing one Thing, so it's only ever in a single state.  Each state knows how to handle input, and knows which state comes next, sort of like how level 2 knows what the jump button does, and also knows that level 3 is up next.

As really rough code, you might end up with something like this:
class State:
    def process(self):
        # should return the next state to be in
        raise NotImplemented


class ProcessingState(State):
    def process(self):
        command = input("> ")
        if "Shut down" == command:
            print("Log out complete.")
            return LoginState()
        print("Unrecognized command.")
        return self


class LoginState(State):
    def __init__(self):
        self.required_password = "123"
        print("Greetings User")

    def process(self):
        pword = input("Please enter passcode: ")
        if pword == self.required_password:
            print("Password expected")
            return ProcessingState()
        print("Password Check Failed")
        return self


current_state = LoginState()
while True:
    current_state = current_state.process()
Reply
#3
Another option would be to create a simple menu driven system, which ichabod801 has an excellant tutorial on located here: Command Line Interface.

As to your code, there are numerous errors, some serious, some not so much. It is not a good idea to use variables that are used by Python, even if you capitalize it, as you did with "Input". Variables should be lower case, for example "required_password", not "Required_Password". Please do not use single letters as variables, they become difficult to follow and give no clue as to their purpose.

In your code you assign "Name" as "User" (line 8) and "B" a value of "/:" (line 12), then on line 25 you assign "C" the value of Name + B, making "C" equal to "User/:". Things are now spiraling out of control towards a fiery crash. For instance, you never allow the user to change the value of "Name", it will always and forever be "User", never "Admin" or "Larry" or "Moe" or "Curly". A better way would be to ask for the name, rather than hard code it:

name = input("Please enter your name: ")
There are numerous other errors in your logic that need to be addressed before your program will come close to running.  Might I suggest you start by creating a flow chart, showing what you want to do and when you want it to happen. You can then add variables, functions or classes to your chart and finally convert it to actual code.

Whether you choose to go the route suggested by nilamo or that of ichabod801, you need to understand what is happening, when it is happening and why it is happening.  To that end, be liberal with the use of the "print()" function.  They can always be removed once your code is finalized.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
Thank you for all the help, I've got the file working now
Reply
#5
(Aug-23-2017, 02:50 PM)Contraversia Wrote: Thank you for all the help, I've got the file working now?

I don't know, do you?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The supplied user buffer is not valid for the requested operation. py2exe 1 3,311 Apr-27-2018, 12:17 AM
Last Post: py2exe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020