Home > database >  Problem returning an input value within a while loop in Python
Problem returning an input value within a while loop in Python

Time:07-04

I have a question with my code in Python. I want to return a value for the Input.input_pos() function within this class. if I type one of the conditional values when I am asked to input while running the program, it returns correctly, but if I input something else and the else condition or the exception occurs, the function loops (which I meant to do that), but when I type again one of the conditional values (either 6, 7, 8, 9 or 10), it doesn't return any value anymore. Why is that?

I am sure it doesn't return a value because I use that value for another function and also when I print it, it shows None in the console.

I tried using the continue keyword after calling again the function within the class. That kinda fixes the problem, but makes the function to loop itself as many time as I input something out of the conditional values.

Can someone tell how can I make this not to occur and instead return the value even when it has looped?

class Input():
    def input_pos():
        while True:
            try:
                postura = int(input("Postura "))
                if postura == 6:
                    return 6
                elif postura == 7:
                    return 7
                elif postura == 8:
                    return 8
                elif postura == 9:
                    return 9
                elif postura == 10:
                    return 10
                else:
                    print("debes escribir un número del 6 al 10")
                    Input.input_pos()

            except ValueError:
                print("Error, debes elegir un número del 6 al 10")
                Input.input_pos()
            except Exception:
                print("Error, debes elegir un número del 6 al 10")
                Input.input_pos()
            break

CodePudding user response:

Try using while to reassign the value until the input is successful and then the loop will jump out and return

class Input:
    def input_pos(self):

        postura = input("Postura: ")

        while postura not in ["6", "7", "8", "9", "10"]:
            postura = input("debes escribir un número del 6 al 10: ")
        return int(postura)


print(Input().input_pos())

Output:

Postura: 1
debes escribir un número del 6 al 10: 4
debes escribir un número del 6 al 10: 67
debes escribir un número del 6 al 10: 6
6

CodePudding user response:

Try this:

class Input():
    def input_pos(self):
        while True:
            try:
                postura = int(input("Postura "))
                if 6 <= postura <= 10:
                    return postura
                else:
                    print("debes escribir un número del 6 al 10")
                    return self.input_pos()
            except Exception:
                print("Error, debes elegir un número del 6 al 10")
                return self.input_pos()
            break
print(Input.input_pos())

Output:

Postura as
Error, debes elegir un número del 6 al 10
Postura 2
debes escribir un número del 6 al 10
Postura 11
debes escribir un número del 6 al 10
Postura 8
8
  • Related