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 Gather Information

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,773 Points

Is assigning the int in a different place ok? it works

Hi,

Is it possible that this is less smelly than Craig's code? ;)

ticket_request=input("Hi {}, how many tickets would you like to buy? ".format(user_name)) total_price=int(ticket_request)*TICKET_PRICE

rather than assigning the int in a separate line?

This seems fine, but be sure to do this every time when you need to re-use ticket_request. If you do need to use it in other calculations, it'll be easier to turn it in to an integer in a separate line.

1 Answer

Steven Parker
Steven Parker
210,425 Points

It works, but as "ad15" suggested, you would need to call "int" again each time "ticket_request" was used in the code. So doing it just once immediately on input might help keep the code "DRY" if the functionality is extended in the future.

And from the standpoint of clarity, it also changes what "ticket_request" represents. Does it make more sense for it to be a string, or a numeric value?

If you just want to avoid a separate line, you can combine the input and the conversion in one:

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