0

I am currently working on trying to make my own custom shell in python and i am running into a problem with my if, elif chain when i enter user input it will run the first if statement even when the input is not what I want.

import subprocess as sb

# the problem is that the subprocess module casues the if statements to run, somehow 

cwd = sb.run(['echo','%cd%'], shell=True, capture_output=True, text=True)
ls = sb.run('dir', shell=True, capture_output=True, text=True)
while True:
   user = input('>>> ')
   if user == "ls" or "dir":
       print(ls.stdout)
   elif user == "pwd" or "cwd": 
       print(cwd.stdout)
   elif user == "exit":   
       exit()
   else:
       print("invalid command")

even when i put anything into the input it will run the first if statement command even though the user input is not equal to the 'ls' or the pwd command.

  • `if user == "ls" or "dir":`: use `if user == "ls" or user == "dir":` instead, or alternatively, `if user in ["ls", "dir"]:`. Similar for the line below. – 9769953 Oct 01 '22 at 02:15

0 Answers0