Python Python Basics All Together Now Gather Information

Enrica Fedeli-Jaques
Enrica Fedeli-Jaques
6,772 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?

ad15
ad15
2,215 Points

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
186,966 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)))