so i am a beginner in coding and i was kinda making a simple terminal in Python for Practice.
and So i saw the history keyword in Linux and with that you can see the recent commands that you used in Linux's terminal. And i wanna Recreate that in my own but I Don't Know How. everytime i think about it i feel it's so Complicated to do that.
My Source Code :
import os
import math
import platform
from random import random
import shutil
import random
import psutil
balance = 0
balance = float(balance)
def calc():
while True:
op = (input('''
ASTRO-CALC 1.0
Enter a Choice
1. Adding number
2. Subtracting numbers
3. Multiplying
4. Dividing
5. Cube
6. Power
7. Square root
8. BACK TO ASTRO
'''))
if op == '1':
a = float(input("Enter Number 1 : "))
b = float(input("Enter Number 2 : "))
ans = a + b
print("Answer is : ",ans)
elif op == '2':
a = float(input("Enter Number 1 : "))
b = float(input("Enter Number 2 : "))
ans = a - b
print("Answer is : ",ans)
elif op == '3':
a = float(input("Enter Number 1 : "))
b = float(input("Enter Number 2 : "))
ans = a * b
print("Answer is : ",ans)
elif op == '4':
a = float(input("Enter Number 1 : "))
b = float(input("Enter Number 2 : "))
ans = a / b
print("Answer is : ",ans)
elif op == '5':
a = float(input("Enter a Number to cube : "))
ans = a * a * a
print("Cubed number is : ",ans)
elif op == '6':
a = float(input("Enter Base : "))
b = float(input("Enter Exponent : "))
ans = a ** b
print("Answer is : ",ans)
elif op == '7':
a = float(input("Enter a number to square root : "))
ans = math.sqrt(a)
print("Answer is : ",ans)
elif op == '8':
print("GOT BACK TO ASTRO!")
break
def restart():
inputs = (input("ASTRO - are you sure you want to restart your computer?(Y/n) : ")).lower()
if inputs == 'y':
print("ASTRO - SHUTING DOWN..")
os.system("shutdown /r /t 1")
elif inputs == 'n':
print("ASTRO - OPERATION CANCELED")
main()
def shutdown():
inputs = (input("ASTRO - are you sure you want to turn off your computer?(Y/n) : ")).lower()
if inputs == 'y':
print("ASTRO - SHUTING DOWN..")
os.system("shutdown /s /t 1")
elif inputs == 'n':
print("ASTRO - OPERATION CANCELED")
def osver():
print(platform.platform())
print(platform.system())
print(platform.release())
print(platform.version())
print(platform.version().split('.')[2])
print(platform.machine())
def help():
print('''
del - deletes files
cd - shows current directory
cdc - current directory change
calc - opens a console-based calculator with 7 simple keywords
open - opens a specific file with a specific format(document files or executable files)
restart - restarts your system
shutdown - shutsdown your system
osver - opens winver to see what version you have of windows
exit - exits ASTRO
''')
def cdc():
try:
cdcinput = (input("PATH << "))
os.chdir(cdcinput)
print("ASTRO - CURRENT WORKING DIRECTORY : {0}\n\n".format(cdcinput))
except OSError:
print("ASTRO ERROR : INVALID PATH ")
def cd():
cwd = os.getcwd()
print("ASTRO - CURRENT WORKING DIRECTORY : {0}\n\n".format(cwd))
def deleb():
deletingop = (input("ASTRO - enter operation (folder,file,folder_files) ")).lower()
if deletingop == 'folder':
deleteinputfo = str(input("PATH << "))
if os.path.exists(deleteinputfo):
print("ASTRO - FOLDER FOUND")
print("ASTRO - NO FILES IN FOLDER")
os.rmdir(deleteinputfo)
else:
print("ASTRO - INVALID PATH OR FOLDER DOES NOT EXIST")
if deletingop == 'folder_files':
pathshutil = str(input("PATH << "))
if os.path.exists(pathshutil):
print("ASTRO - FOLDER FOUND")
print("ASTRO - FILES IN FOLDER FOUND")
shutil.rmtree(pathshutil)
print("DELETED '",pathshutil,"' FOLDER WITH THE FILES IN IT")
else:
print("ASTRO - INVALID PATH OR FOLDER WITH FILES DOES NOT EXIST")
if deletingop == 'file':
deleteinput = str(input("PATH << "))
if os.path.exists(deleteinput):
print("ASTRO - FILE EXIST")
os.remove(deleteinput)
print("ASTRO - DELETED : ",deleteinput)
else:
print("ASTRO - ERROR : FILE DOES NOT EXIST OR INVALID PATH. ")
def exita():
while True:
areyousure = (input("are you sure ? (Y/n) : ")).lower()
if areyousure == 'y':
print("\n\nASTRO - exit (EXECUTED)")
break
elif areyousure == 'n':
print("BACK TO ASTRO")
break
else:
print("ASTRO - EXPECTED (Y/n)")
def openx():
startfileinput = str(input("PATH << "))
if os.path.exists(startfileinput):
os.startfile(startfileinput)
else:
print("ASTRO - INVALID PATH OR FILE DOES NOT EXIST")
def renamer():
renameinputpath = str(input("PATH << "))
renameinput = str(input("RENAME : "))
if os.path.exists(renameinputpath):
print("ASTRO - FILE EXIST")
os.rename(renameinputpath,renameinput)
print("ASTRO - ",renameinputpath," CHANGED TO ",renameinput)
else:
print("ASTRO - ERROR : INVALID PATH OR FILE DOES NOT EXIST")
def reading():
readpath = str(input("PATH << "))
if os.path.exists(readpath):
fileread = open(readpath,'r')
contentss = fileread.read()
print(contentss)
while True:
done = str(input("CLOSE = (c) : ")).lower()
if done == 'c':
fileread.close()
break
else:
print("ASTRO - EXPECTED C NOT '",done,"'")
def randomg():
random_number = random.randint(1,100)
tries = 0
controloop = True
while controloop:
inputr = int(input("There is a random number between 1 and 100, Guess it! : "))
if inputr != random_number and inputr < random_number:
print("Too Low! ")
tries += 1
elif inputr != random_number and inputr > random_number:
print("Too High! ")
tries += 1
if inputr == random_number:
print("CORRECT! number was : ",random_number,"and the number of tries it took you : ",tries)
playagain = str(input('play again? (Y/n) ')).lower()
if playagain == 'y':
print("Lets goo!")
elif playagain == 'n':
print("ASTRO - BACK TO ASTRO !")
controloop = False
def Deposit():
while True:
try:
howmuch = float(input("Deposit : "))
balance += howmuch
break
except ValueError:
print("Please Enter Only Numbers")
def bank():
def Withdraw():
try:
howmucht = float(input("Withdraw : "))
if balance < howmucht:
print("Sorry you dont have enough money to take please deposit some money or get a job")
elif balance > howmucht:
balance -= howmucht
return balance
except ValueError:
print("Please Enter Only numbers ")
def ShowBalance():
print("Your Balance is : $",balance)
holder = True
while holder:
mainchoice = int(input('''
ASTRO-BANK
Enter a Choice :
1. Deposit
2. Withdraw
3. Show Balance
4. BACK TO ASTRO
'''))
if mainchoice == 1:
Deposit()
elif mainchoice == 2:
Withdraw()
elif mainchoice == 3:
ShowBalance()
elif mainchoice == 4:
print("ASTRO - BACK TO ASTRO")
def cpu_u():
l1, l2, l3 = psutil.getloadavg()
CPU_use = (l3/os.cpu_count()) * 100
print(CPU_use)
print("*************")
print("ASTRO 1.0. made by ali obaidi")
print("*************")
def main():
try:
while True:
# main input :
maininput = (input(">> ")).lower()
# file opening
if maininput == 'open':
openx()
# how to exit
if maininput == 'exit':
exita()
break
if maininput == 'calc':
calc()
print("ASTRO - Welcome back to Astro!")
if maininput == 'del':
deleb()
if maininput == 'cd':
cd()
if maininput == 'cdc':
cdc()
if maininput == '--help':
help()
if maininput == 'osver':
osver()
if maininput == 'shutdown':
shutdown()
if maininput == 'restart':
restart()
if maininput == 'rename':
renamer()
if maininput == 'read':
reading()
if maininput == 'randgame':
randomg()
if maininput == 'astrobank':
bank()
if maininput == 'cpuu':
cpu_u()
except KeyboardInterrupt:
print("ASTRO - (KEYBOARD REACTION EXAMPLE OF CTRL + C)")
except PermissionError:
print("ASTRO - PERMISSION DENIED")
main()
You are supposed to look in the main function at the bottom to see the keywords.
in the main function every keyword that maininput variable gets has its own function call but if you enter something different from the keywords i have written maininput variable doesn't do Anything.
now i want to add the keyword History and write its function to see your recent keywords or commands you used so please Help me add That :).