# python coding project-Angry Birds

• (2 Pages)
• 1
• 2

## 15 Replies - 5242 Views - Last Post: 13 September 2012 - 09:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=291905&amp;s=83b806a2c81bf4133dd4b66659207408&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

# python coding project-Angry Birds

Posted 13 September 2012 - 08:32 PM

This is the project:

you have to write a Python function named horizontalHit that has 3 input parameters:

The first one is d1, which tells the distance from the left end of the pool to the stage.
The second one is d2, which tells the distance from the right end of the pool to the stage.
The third one is h, which tells the height of the stage.
Based on our physics knowledge, we know that we can only get the time first so we can get the velocity later. To calculate how long it takes to hit the pig, here is the formula:

h =v0*t + 0.5*g*t^2
where v0 is the vertical velocity of the bird. Assume that the bird is flying horizontally, and hence its vertical velocity is 0. Thus, we will use the following formula:

h = 0.5*g*t^2.
t = sqrt(2h/g)
Here g is the gravitational constant, which has a value of 9.8.

To get the velocity, we know that distance = v * t. Also, here, since the pig is in the middle of the pool, we need to use the following formula:

v = (d1+d2)/(2t)
Finally, after you get the time and the velocity, print them out in the function.

HERE IS MY CODE I STARTED and I DONT NO WHERE I AM GOING WRONG?
```def horizontalHit(d1,d2,h):
g=9.8
h=0.5*g*(t^2)
t=math.sqrt((2*h)/g)
v=(d1+d2)/2
d=v*t
print("")
```

This post has been edited by modi123_1: 13 September 2012 - 09:36 PM
Reason for edit:: highlight your text and click the 'code' button in the editor bar.

Is This A Good Question/Topic? 0

## Replies To: python coding project-Angry Birds

### #2 xclite

• I wrote you an code

Reputation: 954
• Posts: 3,340
• Joined: 12-May 09

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:02 PM

Well, the part where you print an empty string probably doesn't help.

Also, please tell us what your specific problem is rather than "it doesn't work!!?!?oneoneone"

### #3 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:07 PM

I honestly dont know what you mean by ut my source between code tags.

My problem is that I am trying to understand what the instructions is asking me to do. I am confused as to what I am suppose to be printing at the end.

### #4 xclite

• I wrote you an code

Reputation: 954
• Posts: 3,340
• Joined: 12-May 09

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:08 PM

In the box, where you type the words you're saying, there are instructions about how to put code tags around your code. If I had the ability I would edit it for you, but I don't spend a lot of time on the Python forums.

First, you should be writing a function that takes the three parameters. As your function calculates the time and velocity, output them with a print statement.

This post has been edited by xclite: 13 September 2012 - 09:10 PM

### #5 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:09 PM

```def horizontalHit(d1,d2,h):
g=9.8
h=0.5*g*(t^2)
t=math.sqrt((2*h)/g)
v=(d1+d2)/2
d=v*t
print("")
```

### #6 xclite

• I wrote you an code

Reputation: 954
• Posts: 3,340
• Joined: 12-May 09

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:11 PM

That's better, I actually noticed the function definition that time.

I'm pretty sure python is whitespace sensitive - shouldn't the lines in the function be indented?

### #7 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:13 PM

yes it is should be indented. I have it indented in my python IDLE but here is the correction:

```def horizontalHit(d1,d2,h):
g=9.8
h=0.5*g*(t^2)
t=math.sqrt((2*h)/g)
v=(d1+d2)/2
d=v*t
print("")
```

### #8 xclite

• I wrote you an code

Reputation: 954
• Posts: 3,340
• Joined: 12-May 09

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:16 PM

It may seem petty, but the thing is the sample code you post when asking for help should be identical to what you're running, so that there are no minute differences causing problems.

Now that we have indented code, you're doing part of what you're supposed to. Remember when I said you had to output the time and velocity as you calculate them (got that straight from the directions, by the way...)? You should... try that? Use print statements to output t and v after you calculate them.

I'm sure what (if anything) the function is supposed to return given the directions we have to work with.

This post has been edited by xclite: 13 September 2012 - 09:19 PM

### #9 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:19 PM

```print("Velocty:",v)
print("Time:",t)
```

### #10 xclite

• I wrote you an code

Reputation: 954
• Posts: 3,340
• Joined: 12-May 09

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:20 PM

These should be part of the function, also, not just random lines you type out.

### #11 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:24 PM

I did not purposely indent the second print statement. I am new to this website so I didnt know if I was suppose to space it out after I hit enter. Anyway:

```def horizontalHit(d1,d2,h):
g=9.8
h=0.5*g*(t^2)
t=math.sqrt((2*h)/g)
v=(d1+d2)/2
d=v*t
print("Velcity:",v)
print("Time:",t)
```

### #12 xclite

• I wrote you an code

Reputation: 954
• Posts: 3,340
• Joined: 12-May 09

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:32 PM

So, given some sample data, does it work?

### #13 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:33 PM

No it is giving a traceback error

### #14 xclite

• I wrote you an code

Reputation: 954
• Posts: 3,340
• Joined: 12-May 09

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:35 PM

I want to help you, but this is like pulling teeth. What is the traceback error?

### #15 kehara15

Reputation: 0
• Posts: 42
• Joined: 13-September 12

## Re: python coding project-Angry Birds

Posted 13 September 2012 - 09:36 PM

Ok so the error was because of the t^2 so I changed it to t*t and it works now. Can i post the second part of the project with the code that I have started