-1

I have a program that is executed. After that, the user has an option to load back their previous input() answers or to create a new one (just re-execute the program). The program is based on user input and so if I wanted to reload the user's previous inputs, is there a way to pre code an input with the user's previous answer? As a crude example of what my program does I just made this:

def program():
    raw_input=input()
    print("Would you like to reload previous program (press 1) or create new? (press 2)")
    raw_input2=input()
    if raw_input2==2:
        program()
    elif raw_input2==1:
        #And here is where I would want to print another input with the saved 'raw_input' already loaded into the input box.
    else:
        print("Not valid")

For any confusion this is my original code:

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("\033[1m"+"*"*115+"\033[0m")
    for cmd in commands:
        execute_command(cmd)
        line_numbers+=1

I tried creating a Python-like coding program using Python which generates new inputs (new lines) until you want to end your program by entering in nothing. This is only a snippet of my code of course. The problem I'm having is that after the user finished writing their basic program, whether you got an error because your code didn't make send or if it actually worked, I want there to be a 'cutscene' where it asks the user if they want to reload their previous program (because rewriting you program everytime there's an error is hard). I'm just asking whether I can do something like this: If my program was print("Hi"), and I wanted to reload it, I want to get an input; raw_input=input() but I want print("Hi") already inside the input().

BattleCalls
  • 63
  • 1
  • 7
  • Could you explain a bit more what should happen in the `==1` case? What other input? Do you want to append the new input to raw_input? – Raphael Feb 14 '22 at 15:55
  • 1
    Why do you compare a string (result of input() ) with a number (==1, ==2) ? – Patrick Artner Feb 14 '22 at 15:56
  • Why do you recurse on ==2 to do the same exact things again? – Patrick Artner Feb 14 '22 at 15:56
  • 1
    Where do you think this "previous input" would come from? – Scott Hunter Feb 14 '22 at 15:57
  • [whats-the-official-way-of-storing-settings-for-python-programs](https://stackoverflow.com/questions/965694/whats-the-official-way-of-storing-settings-for-python-programs) – Patrick Artner Feb 14 '22 at 16:00
  • [load-parameters-from-a-file-in-python](https://stackoverflow.com/questions/8525765/load-parameters-from-a-file-in-python) – Patrick Artner Feb 14 '22 at 16:01
  • Essentially you store your wanted data in what form whatsoever in a file and load it again. Plenty of ressources on the web and SO to do that. – Patrick Artner Feb 14 '22 at 16:02
  • why do you never use `raw_input` ? – Patrick Artner Feb 14 '22 at 16:03
  • I'm relatively new to Python but I have the ==2 and ==1 thing was just a demo I created in five seconds. I have all the variables down but I just want to know if printing an input with some information already in it is possible; kind of like auto-fill passwords on computers. – BattleCalls Feb 14 '22 at 16:07

2 Answers2

0

This is not possible with the built-in input function. You will need a more fully-featured CLI library like readline or prompt-toolkit.

0x5453
  • 12,753
  • 1
  • 32
  • 61
0

Some notes:

  • The input function always returns strings.
  • The while True keeps going until either 1 or 2 is entered
  • You can pass a string with the input-function call to specify what input is expexted.
  • If you put this in a loop you could re-ask the question, and you should then update the prev_answer variable each iteration.
prev_answer = 'abc'

while True:
    raw_input2=input("Would you like to reload previous program (press 1) or create new? (press 2)")
    if raw_input2=='2':
        user_input = input('Please give your new input')
    elif raw_input2=='1':
        user_input = prev_answer
    if raw_input2 in ('1','2'):
        break


print(user_input)

Based on edit:

prev_answer = 'abc'

while True:
    raw_input2=input("Would you like to reload previous program (press 1) or create new? (press 2)")
    if raw_input2=='2':
        user_input = input('Please give your new input')
    elif raw_input2=='1':
        print(prev_answer)
        user_input = prev_answer + ' ' + input()
    if raw_input2 in ('1','2'):
        break
nessuno
  • 118
  • 5