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

Python

Y G
Y G
1,440 Points

Why does the int function not work when I use it with the following code?

To clarify...every time I put in the number three in words instead of a number, I get an error message. Isn't the int function supposed to extract that to the number "3"?

TICKET_PRICE = 10

tickets_remaining = 100

print("There are {} tickets remaining.".format(tickets_remaining))

name = input("What is your name? ")

print("Hello, {}. ".format(name)) number_of_tickets = input("How many tickets would you like? ") number_of_tickets = int(number_of_tickets)

price_of_tickets = number_of_tickets * TICKET_PRICE

print("The total price for your tickets is ${}.".format(price_of_tickets))

2 Answers

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

No, the int function tries to convert whatever is placed in its parentheses to an integer. Examples:

  • int("3") would return 3
  • int("test") would return an error as that can't be converted to a numeric value.
  • int("three") would also return an error as python can't map the written word of a number to its value.
Y G
Y G
1,440 Points

Ah okay! So there is no way to convert the word "three" into the number 3 in python? (assuming this is for user input). I would just have to create an error message or "exception" to let them know they need to put in a numeral instead of writing out the number?