• entries
7
2
• views
886

# an early coding exercise 1

490 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

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

• I've been trying different algorithms, and just yesterday I adapted one from the Graphics Programmer's Black Book (the chapter 17), and it works... but doesn't wrap around the edges like the other algorithms do. It does vertically, but not horizontally. I'm doing the wrapping by using an extra outer border of cells all around, that each gets a copy of the opposite inner border.
I've been trying for hours to figure out why it isn't wrapping around but I got nowhere so far. Meanwhile I also burned out.
If someone else could take a look and see if they could figure out what's wrong, I'd appreciate it a lot. A fresh pair of eyes might see better than mine. I don't know if I should paste the code right here, as it's a little long (some 200 lines), so meanwhile it's in this repo right here. It's a simple console app, works on the windows console (don't know about the linux terminal).
There's two generation algorithms there for comparison, and you can easily switch using the algo variable. The SUM works fine, the BITS is the one that doesn't. Not sure what else to say. I tried commenting the code for clarity.
Well, if someone has 5 minutes to spare, I'll greatly appreciate it. Thanks in advance.
----------
EDIT: A specific symptom that I noticed (that I didn't think to mention earlier) is that when using the BITS algorithm (which uses bit manipulation, hence the name of the flag), the cells on the outter edges don't seem to be affected by this part of the code that kills a cell or the equivalent part to revive a cell (specifically the "[i-1]" and "[i+1]" lines, which should affect the cells to the sides of the cell being considered):
# if it's alive if cellmaps[prev][j][i] & 0x01: # kill it if it doesn't have 2 or 3 neighbors if (n != 2) and (n != 3): cellmaps[curr][j][i] &= ~0x01 alive_cells -= 1 # inform neighbors this cell is dead cellmaps[curr][ j-1 ][ i-1 ] -= 2 cellmaps[curr][ j-1 ][ i ] -= 2 cellmaps[curr][ j-1 ][ i+1 ] -= 2 cellmaps[curr][ j ][ i-1 ] -= 2 cellmaps[curr][ j ][ i+1 ] -= 2 cellmaps[curr][ j+1 ][ i-1 ] -= 2 cellmaps[curr][ j+1 ][ i ] -= 2 cellmaps[curr][ j+1 ][ i+1 ] -= 2 The actual effect is that the leftmost and rightmost edges are always clear (actually, as depicted, the ones on the opposite side to where the actual glider is, are affected, but not the ones next to the glider).
when a glider approaches the edge, for exampple, this edge should have 1 cell And the opposite side should v like this not be like this, but this V v V V | . . . . | . . . . . . . .| . . . .| | . . # . | . . # . . . . .| . . . .| | . # . . | # # . . . . . #| . . # #| | . # # . | . # # . . . . #| . . . #| | . . . . | . . . . . . . .| . . . .| | . . . . | . . . . . . . .| . . . .|

×