0
public static double readNumber(String prompt,double min, double max){
 Scanner scanner = new Scanner(System.in);
 double value;
 while (true){
 System.out.print(prompt);
 value = scanner.nextFloat();
 if (value >= min && value <= max) {
 break;
 } else
 System.out.println("Enter a value between "+min+" and "+max);
 }
 return value;
}

The upper one works. But the following one doesn't jump out of the loop.

def readnumber(prompt, minimum, maximum):
 while True:
 value = float(input(prompt))
 if minimum <= value <= maximum:
 return value
 else:
 print(f"a valid value needed between {minimum} and {maximum}")
 break

It doesn't work the same way. HELP the new beginner please

asked Nov 14, 2022 at 16:05
5
  • can you please explain what exactly this method do Commented Nov 14, 2022 at 16:09
  • if I input a number into this method then it should be between the min and max values, if not it will give a hint and ask you to input it again Commented Nov 14, 2022 at 16:16
  • When I run your Python example, it always breaks out of the loop after prompting one time. If the value is between the minimum and the maximum, it executes the return value statement, and the function returns the value. Otherwise, it executes the break statement, and the function returns None. Are you sure that the code you copied into your question is the same as the code that you are trying to run? Commented Nov 14, 2022 at 16:17
  • 3
    @HYZ just remove the break statement Commented Nov 14, 2022 at 16:20
  • what's wrong with putting in a bit of print statements or using a debugger, before posting here? there's nothing all that specific to Python going on. welcome aboard, but try to aim for a bit less trivial questions in the future. Commented Nov 14, 2022 at 17:23

3 Answers 3

1

You have the break in the wrong case. in the java method you break when the value is > minimum and < than maximum, but in the python code you break the loop in the other case

answered Nov 14, 2022 at 16:17
Sign up to request clarification or add additional context in comments.

Comments

0

For me it works perfectly fine. To use the function just put it somewhere and make sure to make the prompt a string like for example in this usecase of the function:

print(readnumber("input a number: ", 2, 4))

Edit: For contiuos asking for a number remove the break statement in the else clause

answered Nov 14, 2022 at 16:16

3 Comments

It's supposed to continue prompting for input until the user types a number in the given range. Did you try entering a number that was out-of-range?
But if I input the wrong number how I can make it return to let me input the correct number again? Thanks
@HYZ remove the break statement
0

You could add an extra line of code and create a flag and change the flag e.g.,

flag = true

and use

while(flag)

Then instead of break, you can change the flag to false so the loop stops explicitly rather than implicitly

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.