• entries
7
2
• views
963

# an early coding exercise 1

518 views

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 ()

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

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

## Create an account

Register a new account

• ### Similar Content

• Hello all,
essentially what I'm looking for is a flowchart generator that I can leverage with my python applications using, "magic characters" (@) and the like, above flow control structures, and important bits of code. While there does seem to exist some similar libraries in the py eco system, there doesn't seem to be anything 1:1 to what I'm looking for, I've heard of Doxygen, but i'd prefer something native to Python.
Really just something that gives my engineers minimal control of the output of what the flowchart will look like in the most minimally invasive way possible. Also, I know the process for flowcharting is backwards in this proposition, but this is more for management after the fact.
If something exists, great! If not, I'll code it out, but don't want to duplicate efforts if something similar is already out there
Marcus
• By jyscal
Hello
I am trying to create a grid of images in pyglet and python and I am not sure where exactly I am going wrong.
The goal is for it to be a Breakout/Arkanoid clone. The problem I am having is getting the brick images to display in a grid.
Here is the code that as far as I can tell, should place the bricks in the correct position.

class Brick(): def __init__(self, space): # Create the list to hold different sprite bricks and load images self.batch = pyglet.graphics.Batch() self.brick_images = ['brick1.png', 'brick2.png'] self.brick_sprites = [] # 1 out of 5 chance to drop a power pill self.chance_to_drop = 1 # Set the images anchor point to its center and create sprites for i in range(len(self.brick_images)): img = pyglet.image.load(self.brick_images[i]) img.anchor_x = img.width // 2 img.anchor_y = img.height // 2 self.brick_sprites.append(pyglet.sprite.Sprite(img)) for x in range(7): for y in range(7): self.body = pymunk.Body(body_type=pymunk.Body.KINEMATIC) # The position where each pymunk body will be placed self.body.position = x * 100 + 75, y * 30 + 340 self.brick_type = random.randint(0, len(self.brick_sprites) - 1) if self.brick_type == 0: sprite = self.brick_sprites[0] # Set the sprite to the same position as the pymunk body sprite.set_position(self.body.position.x, self.body.position.y) sprite.batch = self.batch elif self.brick_type == 1: sprite = self.brick_sprites[1] sprite.set_position(self.body.position.x, self.body.position.y) sprite.batch = self.batch self.shape = pymunk.Segment(self.body, (0, 0), (50, 0), 6) self.shape.elasticity = 0.80 self.shape.collision_type = collision_types['brick'] space.add(self.body, self.shape) handler = space.add_collision_handler(collision_types['brick'], collision_types['ball']) handler.separate = self.remove_brick
So what I am trying to accomplish is have 7 rows of 7 bricks. As far as I can see the sprites are being created in the loop, but when I run the program only 2 bricks are being displayed. I am sure there is something wrong with the way I am looping but honestly just cannot see where I am going wrong. I have spent some time, trying to see the error but simply cannot see where I am going wrong.
I can see that the pyglet brick sprites are NOT being set to the correct x, y of the pymunk body, even though, using the same formula for the player paddle object lines up the sprite perfectly.

#Set the sprite to pymunk object position self.image = pyglet.image.load('paddle.png') self.image.anchor_x = self.image.width // 2 self.image.anchor_y = self.image.height // 2 self.sprite = pyglet.sprite.Sprite(self.image, x=self.position.x, y=self.position.y)
I am very confused with this one and I just hope I have explained everything clearly enough. Thank you for any help or assistance in any way.

• Hi,
I have an online multiplayer RPG maze game which I built in NodeJS. It is quite simple. Uses socket.io. Single threaded application so I don't have to worry about race conditions such as two users attempting to take the same object at the same time. The client sends in messages, which are processed by the gameserver and alter the player/world state, query the MySQL database, and broadcasting messages to all the other players and also updating things in the MySQL database.
In the last month I have been learning python and I would like to port my game to Python as I think it will be better suited. Doing some research I have found many suggest using Twisted Python so this is the route I am going to take.
I wondered if anyone could give me some suggestions for a basic framework of how I would handle the problem of race conditions when moving to an environment where things are operating in parallel, especially with regard the querying and updating of the state in the MySQL.
If at all possible I wondered if there are any open source implementations of an MMO architecture using Twisted Python that I could look through that dealt with these issues.

• I want to make some games. My question is what should my first steps be, besides learning more python and unity2d?
I'm hoping I haven't asked this before.
I thought my first step was conquering enough of unity and python so I can start making small games.
I don't know enough python to build a text-based adventure game just yet.
I haven't really started with unity 2d yet.
I want to make 4 games:
1)  a survival game set in a bubble in New Zealand
2) an rpg battle game about differently-shaped spaceships protecting their resources
3) a hustling game influenced by land of Illusion: starring mickey mouse graphics and hell's kitchen ds mechanics
4) an end of the world game where you play god and mutate heroes to fight the ultimate evil supervillain

Other games which are major influences include: 8bitMMO, Another World, Discworld 2, Clop, Osmos, Hacker Evolution: Untold, the sims 1, close combat 1, MTGO, Limbo, Doug TenNapel games, Lemmings, Freedom Force, Gain Ground, Gynoug, Joust, Robocop Vs The Terminator, Ecco: the Dolphin, Super Meat Boy and Syphon Filter 1.

Also does anyone have tips about marketing, or should I not worry until I have an early build?

• I'm trying to find/figure out where in a loop, before the loop, after the loop, the computer understands time.
As I have created a loop and I need to put a time-limit on an input.
lost my program, so I'll adapt the following to a guessing game from a coin flip. Then I need to insert a time counter, just need to understand where to put it?

import random
def variables ():
tails = 0
coinCount = 0
againPlay = "y"

def game():
tails = 0
againPlay = "y"
coinCount = 1
while coinCount > 0:
if againPlay != "y":
print (" and ", tails, "tails.")
end = input ("You're all done now!")
nmCoin = random.randrange(2)
if coinCount > 100: againPlay = "n"
if nmCoin == 1:
coinCount = coinCount + 1
elif nmCoin == 0:
tails = tails + 1
coinCount = coinCount + 1
else:
print (" and ", tails, "tails.")
end = input ("You're all done now!")

variables ()
game ()

×