Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    6
  • comments
    2
  • views
    620

an early coding exercise 1

Sign in to follow this  
WinterDragon

432 views

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 ()
Sign in to follow this  


2 Comments


Recommended Comments

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

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!