0

im new to python. I tried to search for this issue but all i found is Java discussions.

i need to print out a selection menu like:

  1. bla bla 1

  2. bla bla 2

  3. bla bla 3

i need it to run until the input is within range but i need it to be a function that takes 3 argument (lower_limit, upper_limit, user_input). that way i could call the function and send it the specific limits i need to check each time.

so i run this code:

number_input = input("\nPlease choose a game to play: ")
game_selected = check_input_between_range(1,3,number_input)

to call for the checking function with the 3 arguments (this time the limits are 1 and 3)

def check_input_between_range(lower_limit, upper_limit, user_input):
try:
    if lower_limit <= int(user_input) <= upper_limit:
        print("user_input is inside the limits")
        return user_input
    else:
        print("input is out of limits\n")
except ValueError:
    print("user_input is not int")
except TypeError:
    print("user_input is not int")
print("check function finished")

but i cant figure out how to make to loop util the values are within range because if i call the function again in the else: block it fires up a different instance of the function so in the end it returns a result for each time the loop was run.

maybe im missing something basic here. will appreciate some assistance.

thanks in advance.

jackhammer
  • 87
  • 10
  • thank you for pointing me in that direction. i'll read that discussion and adapt it into my code (might take me a while lol) – jackhammer Sep 05 '19 at 12:03

1 Answers1

1
def check_input_between_range(lower_limit, upper_limit, user_input):
    try:
        if lower_limit <= int(user_input) <= upper_limit:
            print("user_input is inside the limits")
            return user_input
        else:
            print("input is out of limits\n")
    except ValueError:
        print("user_input is not int")
    except TypeError:
        print("user_input is not int")
    print("check function finished")


lower_limit =1
uper_limit = 3
number_input = input("\nPlease choose a game to play: ")
try:
    number_input = int(number_input)
    while number_input > lower_limit and number_input < uper_limit:
        game_selected = check_input_between_range(lower_limit, uper_limit, number_input)
        number_input = int(input("\nPlease choose a game to play: "))
    if number_input <= lower_limit or number_input >= uper_limit:
        print("input is out of limits\n")
except ValueError:
    print("user_input is not int")
except TypeError:
    print("user_input is not int")

Try this out I just keep your function as it is so that you can understand easily

Here is reduced code with modification in your function

def check_input_between_range(lower_limit, upper_limit, user_input):
    if lower_limit <= int(user_input) <= upper_limit:
        print("user_input is inside the limits")


lower_limit =1
uper_limit = 3
number_input = input("\nPlease choose a game to play: ")
try:
    number_input = int(number_input)
    while number_input > lower_limit and number_input < uper_limit:
        game_selected = check_input_between_range(lower_limit, uper_limit, number_input)
        number_input = int(input("\nPlease choose a game to play: "))
    if number_input <= lower_limit or number_input >= uper_limit:
        print("input is out of limits\n")
except ValueError:
    print("user_input is not int")
except TypeError:
    print("user_input is not int")
Vashdev Heerani
  • 660
  • 7
  • 21