-1

I am learning python from the 'Learn Python the Hard Way' book and at the moment I am tinkering with one of the exercises.

I would like to restart the following function when the nested if statement is True but without repeating the following step (when it asks for the "increment") again. Currently it just does that:

def ask():
    bottom = int(input("Add the lowest element in the list: "))
    top = int(input("Add the highest element in the list: "))
    if top < bottom:
        print("\nThe highest element needs to be greater than the lowest!\n") 
        ask() # This supposed to restart the function
    step = int(input("Add the step by which the list increments: ")) # In case the `if` statement is `True` this step is called twice (or more)
    return(bottom, top, step)

I understand that the ask() command repeats the function inside the if statement and, therefore, when it ends, the function continues from where it was 'stopped'. In other words, it calls first the "increment" step from the if statement and, second, from the function itself following the if statement. Unfortunately I did not find a way to escape this.

You can find the whole code here.

Any help is appreciated.

nocibambi
  • 2,065
  • 1
  • 16
  • 22
  • You need to indent the code inside the function like it is in the pastebin file. Whitespace is very important in Python. – jss367 Oct 07 '17 at 06:48
  • Yes, sorry, I realized only after posting it, the copy/paste erased it. Now I corrected it. – nocibambi Oct 07 '17 at 06:49
  • I saw `while` loop in the linked source. Do you know `break` at your current level of learning Python? Becasue that's all you need to ask the question repeatedly in a loop and leave the loop once you got an acceptable answer. – VPfB Oct 07 '17 at 06:52
  • Yes, actually I have met `break`, but I haven't thought about including a loop inside the function. I will try that one. Though, I am still interested if this is possible to do with an `if` statement for education purposes. – nocibambi Oct 07 '17 at 06:56
  • What if you change your inner `ask` call line to: `return ask() # This supposed to restart the function`? – CristiFati Oct 07 '17 at 08:10
  • @CristiFati: Thank you! Adding the `return` did work. Now I am just wondering why is that? :) – nocibambi Oct 10 '17 at 13:57
  • The function asked for the top and bottom, noticed that they are not correct, then called itself. The inner call, asked again for the top and the bottom, noticed that they are correct , then it required the step. After that, it exited, in the outer call which carried on with its execution requiring the step (for the 2nd time). By adding the `return` the outer call exits immediately without executing the rest of the (outer) function. This works regardless of the nested calls number. – CristiFati Oct 10 '17 at 14:10
  • Yes, this last part was unknown for me. Thank you again! – nocibambi Oct 11 '17 at 16:20

1 Answers1

2

You are going to have to use a while loop (or some sort of loop). You can't use a recursive function unless there is a base case.

Here is one way to do it.

def ask():
    while True:
      bottom = int(input("Add the lowest element in the list: "))
      top = int(input("Add the highest element in the list: "))
      if top > bottom: break
      print("\nThe highest element needs to be greater than the lowest!\n") 
    step = int(input("Add the step by which the list increments: ")) # In case the `if` statement is `True` this step is called twice (or more)
    return(bottom, top, step)
0TTT0
  • 1,288
  • 1
  • 13
  • 23