0

I have a piece of code that stops the loop when nothing is entered but is there a way to make it look more clean and delete the last line that has nothing? It will give me an output like:

**Line 1: print("Hello World"),

Line 2:**

How can I delete that last line(Line 1: print("hello world")? Note, I tried using the \r\033[F thing but it doesn't seem to work for inputs.

line_number=1
command=None
command2=None
command3=None
command4=None
letter_start=0
letter_end=0
command_end=0
textbox_input=None
print_type=1 #1 = string: 2 = variable
commands=[]
variables={}

def execute_command(textbox):
    global print_type
    global commands
    if textbox.startswith('p'):
        if textbox.startswith("pr"):
            if textbox.startswith("print("):
                command=print #You can change what command is used
                if print_type == 1:
                    letter_start=7 #"letter_start" is the variable that holds the position of the first letter (not including parentheses "" or brackets())
                    command_end=5
                elif print_type == 2:
                    letter_start=6
                    command_end=5
            
            else:
                print("Only Print")
        else:
            print("Only Print")
    elif "=" in textbox:
        equals=textbox.index("=")
        variables[textbox(0,equals)]=textbox(equals,-1)
        
    
    if textbox.startswith('(', command_end):
        if textbox.startswith('("', command_end):
            if textbox.endswith(')'):
                if textbox.endswith('")'):
                    print_type=1
                    letter_end=textbox.index(')')-1
                    textbox_input=textbox[letter_start:letter_end]
                else:
                    print(f"-->{textbox}<--: Missing quotes")
            else:
                print(f"-->{textbox}<--: Missing parenthesis") ;
        elif textbox.endswith(')'):
            if textbox.endswith('")'):    
                print(f"-->{textbox}<--: Missing quotes")
            else:
                print_type=2
                letter_end=textbox.index(')')
        else:
            print(f"-->{textbox}<--: Missing parenthesis")
    else:
        print(f"-->{textbox}<--: Missing parenthesis")


#PROGRAM BEGGINING
        
while True:
    textbox1=input(f"    Line {line_number}: ")
    line_number+=1
    if textbox1:
         commands.append(textbox1)
    else:   #if the line is empty finish inputting commands
        break
        
print("--------------")
print(commands)
for cmd in commands:
    execute_command(cmd)

Edit:

according to the comments, here's what questioner want to achieve:

  • Want to remove or edit the previous input.
  • Execution of Python commands through input function.
  • Saving the command typed in the input function in a file.
Faraaz Kurawle
  • 1,085
  • 6
  • 24
BattleCalls
  • 63
  • 1
  • 7
  • Can you provide us with the variable you have used? It really matters what and how you have used the variables. – Faraaz Kurawle Feb 10 '22 at 14:33
  • I edited my question but I have a few variables so I wasn't sure which you were referring to. – BattleCalls Feb 10 '22 at 14:43
  • Thanks for your edit, But its still its not clear what exactly you want to achieve. Do you want to run python command using the `input function` in python. – Faraaz Kurawle Feb 11 '22 at 14:41
  • Normally, when you run the input() function, the computer will process all the code before it and then stop and wait for the user input. In my code, I used code for multiple line inputs because I was trying to recreate Python using Python. The way you can execute the Python you implement into my input() box is by pressing enter when you're done. The problem with this is that it leaves an empty line afterword; for example: `Line 1: print("Hello World")` `line 2: `. My question is how I can delete that last blank line; if possible. – BattleCalls Feb 13 '22 at 07:07
  • you also want to execute the code for example `print('hello world')`, or just want the next `input` line along with the previous text or command. – Faraaz Kurawle Feb 13 '22 at 07:42
  • Yes. Up until the user presses enter, my program documents the previous line inputs and runs a series of programs with them. I want every line to be executed except the last line containing nothing. – BattleCalls Feb 13 '22 at 20:24
  • I have posted an answer, hope it helps. If it helps, then please press the "Tick Button" on my answer. – Faraaz Kurawle Feb 14 '22 at 16:20
  • Thank you lots for your answer. But can you please explain what the subprocess function actually does. I'm not entirely sure what the point of it is. – BattleCalls Feb 14 '22 at 18:37
  • sub-process module allows you to create a new process from python, and also to interact with it. – Faraaz Kurawle Feb 15 '22 at 03:22
  • i have used it in order to lunch python.exe and to interact with it. – Faraaz Kurawle Feb 15 '22 at 03:22

1 Answers1

0

What I understood is you want to create a program which executes Python commands like print('Hello World'), show you the output and also save's it in a document or text file.

for execution of Python command's, you can use sub-process module.

# Code to execute python command
import subprocess
def executor(cmd):
    process=subprocess.Popen(['python.exe'],
                            stdin=subprocess.PIPE, # Input
                            stdout=subprocess.PIPE,# Output
                            stderr=subprocess.PIPE,# Error
                            shell=True)

    results=process.communicate(input=bytes(cmd,'utf-8')) # We can only send data to subprocess in bytes. 
    output=results[0].decode('utf-8') # Decoding or converting output from bytes to string.
    error=results[1].decode('utf-8')# Decoding or converting output from bytes to string.

    return (output,error)
a=executor('''print('hellow')''')
print(a)

To execute commands from input, you can just change a=executor('''print('hellow')''') to a=executor(input()).

and in order to save the previous command in a text file, you can do the following:

  1. Change return (output,error) to return (output,error,cmd) so you can have an instance of text passed in input.
  2. Write it in a file.

To clear the screen after the execution of previous code, just use os.system('cls') # on windows


Here are the site's I used for references:


You might have to make some change in code for your usage, as here on stack overflow, we just give you hints, we can not help you with creation of full program.

Faraaz Kurawle
  • 1,085
  • 6
  • 24