-1

I have the following code:

def five_numbers():
    my_list = []
    for i in range(1, 6):
        user_nr = check_if_number_is_1_to_25(input("Number " + str(i) + ": "))
        my_list.append(user_nr)
    return my_list

def check_if_number_is_1_to_25(number):
    if number.isalpha():
        print("Enter a number between 1 and 25.")
        # Here I want to go back to five_numbers() and the number x (for example number 4)

Now I want to check if the input contains any letters. If it has, I want to print a message and then I want to go back to the number that the user was on earlier. I've tried to return five_numbers() but then the user will start from the beginning.

I appreciate all the help.

Gringo
  • 33
  • 8
  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) ... see the accepted answer. – wwii Nov 30 '18 at 19:08
  • I think the question is asking how to update the iterator, and I didn't see that in the previous linked question. I think this question is closer to the one the user is asking; https://stackoverflow.com/questions/15363138/scope-of-python-variable-in-for-loop – Dan Rayson Nov 30 '18 at 19:45

3 Answers3

1

Add a keyword arg for num and default it to None:

def five_numbers(num=None):
    my_list = []
    if num is None:
        for i in range(1, 6):
            user_nr = check_if_number_is_1_to_25(input("Number " + str(i) + ": "))
            my_list.append(user_nr)
    else:
        # do other stuff with num (4) here...
    return my_list

def check_if_number_is_1_to_25(number):
    if number.isalpha():
        print("Enter a number between 1 and 25.")
        five_numbers(4)
JacobIRR
  • 8,545
  • 8
  • 39
  • 68
  • I don't get it. Why are you typing five_numbers(4) ? I want it to be as a counter. For example, if the user is on number 2, and is typing alpha - I want to go back to the input("Number 2: ") – Gringo Nov 30 '18 at 19:04
  • @Gringo in that case, you could set a global variable outside all the functions and track that between the functions as it changes. – JacobIRR Nov 30 '18 at 20:12
0

You can use a while loop to keep asking the user for a valid input until the user enters one. You should also make the check function raise an exception instead so the caller can catch the exception and retry the input:

def five_numbers():
    my_list = []
    for i in range(1, 6):
        while True:
            user_nr = input("Number " + str(i) + ": ")
            try:
                check_if_number_is_1_to_25(user_nr)
                break
            except ValueError as e:
                print(str(e))
        my_list.append(user_nr)
    return my_list

def check_if_number_is_1_to_25(number):
    if number.isalpha():
        raise ValueError('Enter a number between 1 and 25.')
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

Don't use a for loop, use a while loop with the list length as its condition. Make the check function return a boolean and use it to decide whether to append to the list.

def five_numbers():
    my_list = []
    while len(my_list) < 5:
        user_nr = input("Number {}: ".format(len(my_list)+1))
        if check_if_number_is_1_to_25(user_nr):
            my_list.append(user_nr)
        else:
            print("Enter a number between 1 and 25.")
    return my_list

def check_if_number_is_1_to_25(number):
    return number.isdigit() and (1 <= float(number) <= 25)
wwii
  • 23,232
  • 7
  • 37
  • 77