an early coding exercise 1

Published February 21, 2018
Advertisement
4337f-python-5.jpg?w=300

carPrice = input ("what is the base price of the car?")

tax = int (carPrice) * .125
insurance = 250
totalcarPrice = int (carPrice) + int (insurance) + int (tax)

print ("total cost of your car including: insurance $",insurance,",")
print ("and tax: $",tax," comes to $",totalcarPrice)
input ()

 
This is a program that figures out all your extra costs, when buying a car.
The only mistake I still need to figure out, is what the escape clause is for avoiding having a space at the end of a statement inside a print function.
 
It works fine, the user enters the base cost for the car.
Program calculates the tax and adds a previously decided insurance cost.

Then the program provides the user with both the individual costs, and the total all-inclusive price of the car.

EDIT:
After some research not in-book, it turns out that you can avoid the white spaces in between statements by using the function sep = "", which should be treated as a variable - so not inside the quotation marks of the print function, rather, naked inside the brackets.

So the final program now looks like this:

carPrice = input ("what is the base price of the car?")

tax = int (carPrice) * .125
insurance = 250
totalcarPrice = int (carPrice) + int (insurance) + int (tax)

print ("total cost of your car including: insurance $",insurance,",", sep = "")
print ("and tax: $",tax," comes to $",totalcarPrice, sep = "")

 
input ()
0 likes 2 comments

Comments

Alberth

An alternative is to use format():


print("and tax: ${} comes to ${}".format(tax, totalcarPrice))

Write the string as you want it, with "{}" at places where you want a number. Then add ".format(<values>)" behind the string. The format method creates a new string from your template by finding each "{}", and replacing it by a value listed in the format arguments.

Format can do more, but this will suffice at first

February 22, 2018 05:52 AM
WinterDragon

thanks for the tip, very useful. and thanks for reading.

February 22, 2018 10:44 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement