Python Input Prompt Go Back

后端 未结 2 1891
半阙折子戏
半阙折子戏 2021-01-26 00:35

I have a script which has multiple raw_input statements. I would like to add the ability to enter \'undo\' and allow the user to go back and re-enter the last raw_input value.

2条回答
  •  忘了有多久
    2021-01-26 01:17

    looks like you coded in a wrong way, an input raw cannot be your point ... maybe you should create an unique raw input and keep the entry into an array

    ok....now u post a code example i got it...my suggestion:

    import sys
    
    def do(prompt, step):
        if 'u' in prompt:
            step = step-1
        else:
            step = step+1
    
        if step>1:
            print('Please enter x for x \nPlease enter y for y \nPlease enter c to cancel \nPlease enter u to go back to previous question: ')
        else:
            print('Please enter x for x \nPlease enter y for y \nPlease enter c to cancel: ')
        return step
    
    step = 0
    prompt = ""
    while prompt != 'c':
        step = do(prompt, step)
        prompt = raw_input().lower()
    

提交回复
热议问题