1

May someone assist, i am trying to write a program that evaluate user input. accept three arguments: a prompt, a low acceptable limit, and a high acceptable limit; if the user enters a string that is not an integer value, the function should emit the message Error: wrong input, and ask the user to input the value again; if the user enters a number which falls outside the specified range, the function should emit the message Error: the value is not within permitted range (min..max) and ask the user to input the value again; if the input value is valid, return it as a result. however my code is failing to ask the user to renter the number if they enter number out of range here is my code

class Num_Evaluation:
    def __init__(self, prompt, min, max):
        self.prompt=prompt
        self.min=min
        self.max=max

    def num_check(self):
        while True:
            try:
                self.prompt=int(input(self.prompt))
                assert self.prompt>=self.min and self.prompt<=self.max
                print('The number is :', self.prompt)
                break
            except AssertionError:
                print("Number out of range")

            except: print("only intiger numbers")



user1=Num_Evaluation("enter number in range -10 to 10: ", -10, 10)
print(user1.num_check())

can anyone assist on how to enable the program to keep asking the user to enter the valid number that is within the requred range

John Gordon
  • 29,573
  • 7
  • 33
  • 58

2 Answers2

0

Here's an example of how you can accomplish this.

class Num_Evaluation:
    def __init__(self, prompt, min_val, max_val):
        self.prompt = prompt
        self.min_val = min_val
        self.max_val = max_val

    def num_check(self):
        while True:
            try:
                user_input = int(input(self.prompt))
                if user_input < self.min_val or user_input > self.max_val:
                    raise ValueError("Error: the value is not within the permitted range ({min}..{max})")
                return user_input
            except ValueError as e:
                print(e)
            except:
                print("Error: wrong input")

prompt = "Enter a number in the range -10 to 10: "
user1 = Num_Evaluation(prompt, -10, 10)
print(user1.num_check())
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 16 '23 at 21:08
0

With this line:

self.prompt=int(input(self.prompt))

You are overwriting self.prompt with the user input, so when the user enters an invalid input, the next prompt becomes the integer they entered.

You should also return the user input once it's converted and validated.

So change the variable receiving the user input to something else, and return the validated result.

Change:

self.prompt=int(input(self.prompt))
assert self.prompt>=self.min and self.prompt<=self.max
print('The number is :', self.prompt)
break

to:

number = int(input(self.prompt))
assert number >= self.min and number <= self.max
print('The number is:', number)
return number
blhsing
  • 91,368
  • 6
  • 71
  • 106