Python Python Basics All Together Now Handle Exceptions

my code should work but its giving a starnge output>> How to handle letters as input.

Hi. i want to be able to handle a situation when users enters a number as letters, so i did this:

if not number_of_tickets.isdigit():
            raise ValueError("You Can't enter the number of tickets u want to buy in letters")

I saw this code in this discussion as well:

my code should work but i get this output instead:


help will be appreciated :]

3 Answers

54,186 Points

The code you referenced is different than yours

        number_requested = input("How many tickets would you like to buy, {}?  ".format(name))
        if not number_requested.isdigit():
            raise ValueError("Please enter ticket request as a whole number.")
            number_requested = int(number_requested)
            if number_requested > tickets_remaining:
                raise ValueError("You have requested more tickets than we have!")
    except ValueError as err: 
        print("Oops! Something has gone wrong. {} Try again.".format(err))

In your snapshot you converted number_of_tickets to an integer whereas isdigit() is a string method. The above doesn't convert until the else statement.

How i can mofidy it in my example? i just used a shortcut to do this by cast it to int KRIS NIKOLAISEN

KRIS NIKOLAISEN I understand now that isdigit() is a string method but if i modify it and cast it to an int in the else block other parts of the code break because they can't convert a str to an int

Chris Freeman maybe u can shine some light?