1

Hi and thanks for your time. I need to add the wrong data types in my code that says that it should be an invalid operation when I am using strings. I try to add the break statement in for loop, but I cannot go through. I hope you can help me.

Code:

 def userInput():
    upper = int(input("Enter UppperNumber from column: "))
    if upper < 0:
        print(upper,"Nice, it is in range")
    elif upper < 256:
        print(upper, "Nice, it is in range")
    else:
        print(
            "You must use only numbers which range is : 0 - 255")
        upper = None# we define upperbit as nothing and restart the function, same applies with lowerbot
        userInput()

    lower = int(input("Enter LowerNumber from column: "))
    if lower < 0:
        print(lower, "Nice, it is in range")
    elif lower < 256:
        print(lower, "Nice, it  is in range")
    else:
        print(
            "You must use only numbers which range is : 0 - 255")
        lower_bit_int = None
        userInput()
1
  • Please extract a minimal reproducible example from your code. Also include input (if you can't hardcode it), output and expected output. As a new user here, also take the tour and read How to Ask. Commented May 1, 2021 at 6:43

1 Answer 1

3

add an try except statement to your input and maby instead of recursivly calling your function do it in a while statement. Example:

def userInput():
    while True:
        upper = None
        try:
            upper = int(input("Enter UppperNumber from column: "))
            if 0 < upper < 256:
                print(upper, "Nice, it is in range")
                break
            else:
                raise ValueError
        except ValueError:
            print("You must use only numbers which range is : 0 - 255")
       
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.