Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial<noob />
17,062 Pointsmy 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
54,972 PointsThe 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 />
17,062 PointsKRIS 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 />
17,062 PointsChris Freeman maybe u can shine some light?
<noob />
17,062 Points<noob />
17,062 PointsHow i can mofidy it in my example? i just used a shortcut to do this by cast it to int KRIS NIKOLAISEN