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.
10
  • Can you provide us with the variable you have used? It really matters what and how you have used the variables. Commented Feb 10, 2022 at 14:33
  • I edited my question but I have a few variables so I wasn't sure which you were referring to. Commented Feb 10, 2022 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. Commented Feb 11, 2022 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. Commented Feb 13, 2022 at 7: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. Commented Feb 13, 2022 at 7:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.