An Electronic Safe with Python
In this article, we’ll build a simple safe using Python. This is a CLI application for beginners.
Roland Hewage
In this article, we’ll build a simple safe using Python. This is a CLI application for beginners.
Setting up the safe
The keeper of the safe will first set up the safe with a 6-digit password and the number of maximum attempts a person can attempt in order to access the safe.
Using the safe
The safe will require a 6-digit password to open. Each input has to be taken one by one. When a digit that does not match the corresponding password is entered for the first time, the program has to display “sorry wrong password” and also display the number of attempts left (example “5 attempts left”). It should do the same for following wrong password entries as well.
When all the attempts fail, the program should display “Emergency alarm activated” and end program. When all the passwords are entered correctly, the program should display “safe opening” and end the program.
How to Get Started

First we import python defined native exit function and system, and read the expression and operating system modules. Then we define the ‘startMenu()’ function. Inside the ‘startMenu()’ function, we declare the Global Variables to be used. Then we call the ‘clr()’ function.

Then we classify the traceback limit. Finally, we print the program title and information about the program.
Then we define a new loop. Then we do the below things until an exception occurs. Then we define user inputs choice of function, either 1,2 or 3. If the choice is either 1,2 or 3 then we break out of loop. If a number but not 1,2 or 3 we print “Please only enter numbers on the menu!”.

If a string is entered we print “Please only enter numbers on the menu!”.

If the user input “2” we calls the ‘clr()’ function. User Inputs their login pincode (the function will return).

User inputs their login userAttempts (function will return).


Then we call the export data function.

It opens a file called “password.txt”, and the user pincode and user attempts are encrypted and written to the file.

Then the file is closed. We display the user pincode and the number of user attempts specified by the user.

Finally, the user is taken back to the main menu.

If the user input “1” we call the read data function.

Then we open the file called “password.txt” and the user pincode is decrypted and read from the file.

We call the user login function.

Each input has to be taken one by one. When a digit that does not match the corresponding password is entered for the first time, the program has to display “sorry wrong password” and also display the number of attempts left (example “5 attempts left”). It should do the same for the following wrong password entries as well. When all the attempts fail, the program should display “Emergency alarm activated” and end program.

When all the passwords are entered correctly, the program should display “safe opening” and end the program.
If the user inputs 3, we clear the screen and end the program by executing the endProgram() function.



The whole code of the keySafe.py script is given below.

You can run this application by executing the following code on a terminal.

I hope this article helped you to get started with python by building an Electronic Safe application with python. Keep in touch for more articles. Thank you.
This article was originally published by Roland Hewage on medium
Upvote
Roland Hewage
I'm a Data Science, Machine Learning, Deep Learning, Quantum Computing Enthusiastic.

Related Articles