Skip to content

Instantly share code, notes, and snippets.

@MatrixManAtYrService
Last active January 5, 2020 21:24
Show Gist options
  • Save MatrixManAtYrService/dc4a2eae88bca999ad6ddbf3aa636046 to your computer and use it in GitHub Desktop.
Save MatrixManAtYrService/dc4a2eae88bca999ad6ddbf3aa636046 to your computer and use it in GitHub Desktop.
Python 3.6 f-strings
adult_price = 120
child_price = 60
total_tickets_purchased = 3
adult_ticket = 3
child_ticket = 0
availble_seats = 150
adult_cost=adult_price*adult_ticket
child_cost=child_price*child_ticket
total_cost=adult_cost+child_cost
total_tickets_purchased=(adult_ticket+child_ticket)
seats_left=availble_seats-child_ticket-adult_ticket
# INSTEAD OF THIS
print(" Session summary ")
print("The amount of adult tickets purhcased is",adult_ticket)
print("The amount of child tickets purchased is",child_ticket)
print("The total tickets purchased is",total_tickets_purchased)
print("****************************************************")
print("The total cost of adult ticket is",adult_cost)
print("The total cost of child tickets is",child_cost)
print("The total cost of the tickets is",total_cost)
print("The remaining seats left is",seats_left)
# I WOULD DO THIS
print(f'''
Session summary
The amount of adult tickets purhcased is {adult_ticket}
The amount of child tickets purchased is {child_ticket}
The total tickets purchased is {adult_ticket + child_ticket}
****************************************************
The total cost of adult ticket is {adult_cost}
The total cost of child tickets is {child_cost}
The total cost of the tickets is {total_cost}
The remaining seats left is {seats_left}
''')
@MatrixManAtYrService
Copy link
Author

MatrixManAtYrService commented Jan 3, 2020

I think this looks better, but only do it if you know your code will be run with Python 3.6 or higher.

https://realpython.com/python-f-strings/

Copy link

ghost commented Jan 5, 2020

yeah that's the problem the computers in school run python 3.5 so I wouldn't be able to do it like that but thank you for suggesting it .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment