-1

Hi i'm new to python and i am having issues with exceptions. i have added an exception but it is not working. Below is a sample of my problem.

    if x == '1':


    print("----------------------------------")

    print("1. REQUEST FOR A DOG WALKER")

    print("----------------------------------")

    try:
        print("Select a Date (DD/MM/YY) - eg 12/04/21")

    except ValueError:
        raise ValueError('empty string')
    date = input()
    
    try:
        print("Select a Duration (in hours) - eg 2")
        duration = input()
    except ValueError:
       print('empty string')

    print("Select a breed : 1 - Rottwieler , 2 - Labrador Retriever, 3 - Pitbull, 4 - Other ")

    breed = input()

    print("Number of Dogs - eg 3 ")

    dogsNumber = input()
muji_abkr
  • 11
  • 3
  • you have two exceptions here, first one doesn't take any input. Which one is not working? – Neeraj Apr 11 '21 at 13:56
  • Maybe also tell us what exactly you mean by "Not working" and what your expected behavior is. Then we might be able to help you. You can just edit you question to add the relevant information. Have a nice day! – wuerfelfreak Apr 11 '21 at 14:00
  • the first exception if it's empty or the formate is wrong the program continues – muji_abkr Apr 11 '21 at 14:02
  • This might help you in general: [Asking the user for input until they give a valid response](https://stackoverflow.com/a/23294659/8106583) – wuerfelfreak Apr 11 '21 at 14:03

3 Answers3

1

Whenever you use a try/except statement you are trying to catch something happening in the code the shouldn't have happened. In your example, you are haveing the user enter a number for a duration:

print("Select a Duration (in hours) - eg 2")
duration = input()

At this point, duration might equal something like '2'. If you wanted to add 10 to it then you could not because it is a string(you cannot and int+str). In order to do something like this, you would have to convert the user input to an int. For example:

duration = int(input())

The problem is that if the user enters a letter A, then the program will break when it tries to convert it to an int. Using a try/except statement will catch this behavior and handle it properly. For example:

    try:
        print("Select a Duration (in hours) - eg 2")
        duration = int(input())
    except ValueError:
       print('empty string')
0

1 - You are not checking any conditions to raise the error

2 - You're not catching the ValueError, you have to raise it and then catch in with the except statement.

Try this:

if x == '1':


    print("----------------------------------")

    print("1. REQUEST FOR A DOG WALKER")

    print("----------------------------------")
    
    print("Select a Date (DD/MM/YY) - eg 12/04/21")

    try:
        date = input()
        if len(date) == 0:
           raise ValueError('empty string')
    except ValueError as error:
        print(error)
   
C-Gian
  • 62
  • 2
  • 19
  • 1
    One could also use `assert len(data) > 0` for a more pythonic solution. – wuerfelfreak Apr 11 '21 at 14:05
  • what if it's a wrong format of date inputed or i input an int – muji_abkr Apr 11 '21 at 14:09
  • there are many ways to do this: importing re and using a regex to check the format; importing datetime; or working by hand with strings, this is way more difficult but much more instructive! – C-Gian Apr 11 '21 at 14:15
0

There is nothing in your code that can produce ValueError.

So the except ValueError: is not matched.

saeedkazemi
  • 321
  • 1
  • 5