Python Python Basics All Together Now Handle Exceptions

<noob />
<noob />
16,315 Points

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: https://teamtreehouse.com/community/when-doing-the-valueerror-as-err-it-doesnt-solve-the-how-many-ticket-do-you-want-blue-error-we-solved-earlier

my code should work but i get this output instead: https://gyazo.com/03a380c98022abe0fc997b0c5074ae35

CURRENT SNAPSHOT: https://w.trhou.se/9v50akiocc

help will be appreciated :]

3 Answers

KRIS NIKOLAISEN
PRO
KRIS NIKOLAISEN
Pro Student 49,689 Points

The code you referenced is different than yours

try:
        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.")
        else:
            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.

<noob />
<noob />
16,315 Points

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

<noob />
<noob />
16,315 Points

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

<noob />
<noob />
16,315 Points

Chris Freeman maybe u can shine some light?