Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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: 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

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.

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?