-1
try:
    x = input("Enter the first number:")
    x = int(x)
except:
    print("You have not entered a valid number - 0 has been stored instead")
    del x
    x = 0

This is what I've written, and it runs fine. But, what I actually need to do is check that the input value is between 1 and 9. And I don't know how to do it. This is in Python.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251

3 Answers3

1

You can use this:

x = input("Enter the first number:")
x = int(x)
if x < 1 or x > 9:
    print("You have not entered a valid number - 0 has been stored instead")
    x = 0

Neither try/except or del x is needed is not needed, because no error will be thrown if the x will be less than 1 either more than 9.

You of course can add this and no error will be thrown in this case, but there are keywords like raise that is used in advanced cases and you can handle errors manually. More details: How to use "raise" keyword in Python and Manually raising (throwing) an exception in Python

  • Also, @Petəíŕd The Linux Wizard can you please explain why the try and except function isn't relevant? As I'm just starting out, and it might help anyone else, in my position. – Tashi Higgins Aug 20 '23 at 10:16
0

The code below suits you.

The try/except function is there so you can handle any exception that comes up from the user input (For example: if the user instead of providing a numeric values writes "hello" in the user input prompt the program without the try/except will break with an unhandled exception)

try:
    x = input("Enter the first number:")
    x = int(x) 
    if 1 <= x <= 9: # this checks if the input is in between 1 and 9
        pass # here you can add an action that you want to happen when the if clause is true
    else:
        print("This is not a valid number")
except ValueError:
        print("You have not entered a valid value - 0 has been stored instead")
        x = 0

del x is not needed, as you can just reassign the value of 0 to x after the exception occurs.

Chris
  • 26,361
  • 5
  • 21
  • 42
  • Yeah but the original poster (aka OP, question asker) didn't said that they need to check that user input is exactly a number – Petəíŕd The Linux Wizard Aug 20 '23 at 15:55
  • I've tried running the try/except code, but if I make the user input -5 it doesn't recognize the 'if' statement i.e., that the value is less than 1. " @Petəíŕd The Linux Wizard I need to check that the user input is between 1 and 9 and is an integer. – Tashi Higgins Aug 21 '23 at 06:50
  • @Petəíŕd The Linux Wizard your code worked. But how do I also add/check that the value is an integer and not a string (text, word)? – Tashi Higgins Aug 21 '23 at 06:58
  • @Mechanical Engineer - your code works to make sure that the input is an integer, but it doesn't recognize an error if the input is -5 for example. And the goal is that it does. – Tashi Higgins Aug 21 '23 at 07:10
  • @Tashi Higgins I am pretty sure it does recognize if the number is negative. It's just that I didn't code any action for when that happens. Maybe you could have worded your question a bit better, cause I don't see you mentioning anything about numbers out of the range (1,9). You are specifically saying: "what I actually need to do is check that the input value is between 1 and 9". This is the correct answer. – Mechanical Engineer Aug 27 '23 at 07:26
0

You should be more restrictive in your exception handling: handle only specific exceptions. Your exception handler will be triggered if the input is not an integer, in which case a ValueError exception is raised. You simply need to raise the same exception if a value not between 1 and 9 is entered.

>>> try:
...   x = int(input())
...   if x < 1 or x > 9: raise ValueError
... except ValueError:
...   print("Invalid input: please enter a number between 1 and 9. 0 is assumed.")
...   x = 0
...
56
Invalid input: please enter a number between 1 and 9. 0 is assumed.
>>> x
0
>>> try:
...   x = int(input())
...   if x < 1 or x > 9: raise ValueError
... except ValueError:
...   print("Invalid input: please enter a number between 1 and 9. 0 is assumed.")
...   x = 0
...
4
>>> x
4
Chris
  • 26,361
  • 5
  • 21
  • 42