Skip to content

Instantly share code, notes, and snippets.

@BaileyJM02
Last active March 23, 2018 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaileyJM02/e489310968635e16cfd11dfe74a2ab89 to your computer and use it in GitHub Desktop.
Save BaileyJM02/e489310968635e16cfd11dfe74a2ab89 to your computer and use it in GitHub Desktop.
These need to be run in the same directory (.../var/program.py & .../var/database.py)
# |== NOTICE ==|
# |
# |Made for Tom Smith#0001 on discord. He's a good friend :D
# |Realeased under the MIT Licence
# Array to be imported - think of it as a database
user = {'name': 'Bailey', 'password':'258', 'username': 'Admin'}
# |== NOTICE ==|
# |
# |Made for Tom Smith#0001 on discord. He's a good friend :D
# |Realeased under the MIT Licence
#import the array
from database import user
#Print to command line the information for test purposes
print("IMPORTANT INFO:")
print ("\tName:",user['name'])
print ("\tPassword:",user['password'])
print ("\tUsername:",user['username'])
# Haven't logged in yet
authToken = False
#Function menu() - just prints text to the cmd line
def menu():
print("\n\tMenu\n\n\t[A] Add Car\n\n\t[S] Search\n\n\t[Q] Quit\n")
#Function program() - starts everything, it's the main 'startup' if you like
def program():
#run menu() - from above
menu()
#ask what they want to visit
menuChoice = input("Menu Option: ").upper() #chnage to capital to make it easer in the if statments
#if statment
if menuChoice == "A":
#set var
carryOnA = True
#create loop, while the user wants to carry on, they are given the option to exit at the end
while carryOnA == True:
#var inputs to variables
make = input("Make: ")
model = input("Model: ")
#catch if they enter a non-int
try:
year = int(input("Year: "))
except:
#tell them to use an int next time
print("Please use a number nextime, value '0' as been set!")
#set default
year = 0
#catch if they enter a non-int
try:
price = float(input("Price: "))
except:
#tell them to use an int next time
print("Please use a number nextime, value '0' as been set!")
#set default
price = 0
#try to open the file
try:
toFile = open("carDetails.login", "a") ##extension .login instead of .txt to know that its related to this file in documents, will still open as text file through notepad
#write the data to the file
toFile.write(make+" ")
toFile.write(model+" ")
toFile.write(str(year)+" ")
toFile.write(str(price)+"\n")
#close the file
toFile.close()
# Incase of unknow errors occuring to protect the file
except:
print ("\tERROR: An unknow error occured.")
try:
#try to close the file - if you get here something has gone terribly wrong
toFile.close()
except:
#baso crash
print ("\tERROR: Unable to close file, please exit to prevent system errors!!!")
#ask if the user would like to carry on withi this loop, it will start again if yes
carryOnQuestionA = input("Carry on? [Y/N]: ").upper()
if carryOnQuestionA == "Y":
carryOnA = True #start again
else:
print("Returning...")
program() #restart the program
#next choice
elif menuChoice == "S":
# Doesn't work yet due to experimentation - will probably crash the system or something...
#create empty list
recoveredList = []
#start loop
carryOnB = True
while carryOnB == True:
#open file
Details = open("carDetails.login", "r")
#read all lines
for line in Details:
#add each line to the list
recoveredList.append(line.split())
#print the list
print(recoveredList)
#enter seach term
searchCars = input("Seach car: ")
#find within list
for car in recoveredList:
#tell the user the seach term exists - to be improved
if recoveredList == searchCars:
print("found")
#ask if the user would want to search again
carryOnQuestionB = input("Carry on? [Y/N]: ").upper()
#restart loop
if carryOnQuestionB == "Y":
carryOnB = True
else:
print("Returning...")
program() #restart program
# next menu choice
elif menuChoice == "Q":
print("Coming soon...") # to be added - the quit button
print("Or you can press the red button!") # tell the user to not be lazy
#The option couldn't be found - the user endterd another letter
else:
print("That option doesn't exist!")
#the login function with two variables
def login(username, password):
#if statment
if username == user['username'] and password == user['password']: #user is the list we imported, user['username'] is the item 'username'
#The password matched so we continue and set auth to true
authToken = True
#say hello to the user
print("Hello,", user['name'])
#start the program
program()
#wrong details
else:
print("\nIncorrect username/password. Please re-enter!")
#restart input - can be shown as login(username = input("\nUsername: "), password = input("\nPassword: ")) here...
#...we are setting the variables username and password for the function via an input
login(
username = input("\nUsername: "),
password = input("\nPassword: ")
)
#start the whole god damn thing!!!
login(
username = input("\nUsername: "),
password = input("\nPassword: ")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment