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

having a problem while calculating num_tickets ?

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

why do we need to put 'num_tickets' into 'int'. I believe it is already an 'int' because we assigning the integer value to it like 20,10,2,...so on. I also get confused with the term coerse.

2 Answers

When you receive a value through the input function it is stored as a string in the variable. You have to take that string and convert it to an integer in order to calculate the price; wrapping it in the int function converts the string to an int.

Hi gurveer, this is an important question.

You need to understand that the input method returns a String, not an integer. When you use input and type a number, this number will be stored as a String. To convert or coerce it to an integer, you need the int(...) method. Try this code in the workspace and type in a number ...

user_input = input("Your input: ")
print("The first input is {} ".format(type(user_input)))

input_to_integer = int(user_input)
print("After cenversion or coersion it is {}:".format(type(input_to_integer)))

Does this help to understand the topic?