-4

I am learning about the try and except statements now and I want the program to print out a message when a negative number is inserted but I don't know how to create the order of the statements for this output:

print("how many dogs do you have?")
numDogs= input()
try:
    if int(numDogs)>=4:
        print("that is a lof of dogs")
    else:
        print("that is not that many dogs")
except:
    print("you did not enter a number")

I want the program to print tje output when the user enters a negative number. How can I do it?

martineau
  • 119,623
  • 25
  • 170
  • 301
Ryan
  • 1
  • 1
  • What don't you know? Do you know how to use `elif` statements? Simply after the first if statement insert `elif int(numDogs) < 0: ...`. – code May 31 '22 at 00:10

3 Answers3

0

Here is what you are trying to achieve.

class CustomError(Exception):
    pass

print("how many dogs do you have?")

try:
    numDogs = int(input())
    if numDogs < 0:
        raise CustomError
    elif int(numDogs)>=4:
        print("that is a lot of dogs")
    else:
        print("that is not that many dogs")
except CustomError:
    print("No Negative Dogs")
except:
    print("you did not enter a number") 

You can start by creating a custom exception. See https://www.programiz.com/python-programming/user-defined-exception

After which, you should have a condition to first detect the negative value then raise an error from it.

Finally, You can then create multiple excepts to get different error. The first except is raised on input of negative values (as your condition will do for you) whereas the second is used to catch non valid value such as characters.

Note that your int(input) segment should be WITHIN your try block to detect the error

Jackson
  • 1,213
  • 1
  • 4
  • 14
  • Where in the OP's question does the OP want to raise an error, and a custom one at that, if the user enters a negative number?? – code May 31 '22 at 02:00
0

Just code is like try: if no<0: print("no is negative ")

    raise error 

And further code will be as it is

Jagu
  • 33
  • 4
0

Solution I found is from this SO question

I want the program to print out a message when a negative number is inserted

You can raise a valueError like @Jackson's answer, or you can raise an Exception like in my code below.

    elif numDogs < 0:
        raise Exception("\n\n\nYou entered a negative number")


    def main():
        
        try:
            numDogs= int(input("how many dogs do you have?  "))
            if numDogs >= 4:
                print("that is a lot of dogs")
            elif numDogs < 0:
                raise Exception("\n\n\nYou entered a negative number")
            else:
                print("that is not that many dogs")
        except ValueError:
            print("you did not enter a number")  
    main() 

One thing that I though I should point out is that its probably better practice to just check if the number is < 0 or is NAN. But like you said, you're trying to learn about the try and except statements. I thought it was cool that in this code segment:


    try:
        if numDogs < 0:
            raise ValueError
    except ValueError:
        print("Not a negative Number")

you can throw what error you want and your try will catch it. Guess im still learning a lot too. Only part is, if that happens if your code then one won't know if its a negative number issue or if the number is something like "a", which cannot be converted to one (in this way).

All in all, I think its best to solve this problem with no try / except statements:



    def main():
        
        num_dogs = input("# of dogs: ")
        if not num_dogs.isnumeric():
            return "Error: NAN (not a number)"
    
        num_dogs = int(num_dogs)
    
        return ["thats not a lot of dogs", "that's a lot of dogs!"][num_dogs >= 4] if num_dogs > 0 else "You Entered a negative amount for # of dogs"
    
    
    print(main())


Notice that I wrapped everything in a `main` function, because that way we can return a string with the result; this is important because if we get an error we want to break out of the function, which is what return does.
taylorSeries
  • 505
  • 2
  • 6
  • 18