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 throughinput
function. - Saving the command typed in the
input
function in a file.