Bouncing Ball Distance [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to make a program that allow me to enter a height of a ball to be dropped and put in the number of bounces and give me the total distance. I am having trouble with my equation and see some in-sight please.
h = int(input("Enter hieght"))
b = int(input("Enter Number of Bounces"))
for eachPass in range (b):
d = h * 0.6
if b >= 1:
bounce1 = h + d
if b >= 2:
bounce2 = (d * 0.6) + bounce1 + d
print (bounce2)

While I can't be sure, I believe you want something like this instead
h = int(input("Enter height"))
b = int(input("Enter Number of Bounces"))
bounce1 = 0
bounce2 = 0
d = h * 0.6
for eachPass in range(b):
if eachPass == 1:
bounce1 = h + d
if eachPass == 2:
bounce2 = bounce1 + h + d
print str(bounce2)

Related

I tried to create a basic Slingshot algorithm... Basicly i tried to eliminate every problems in my code but now it doesn't do anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 months ago.
Improve this question
from math import sin, cos
def Sling():
b_h = float(input("Input starting speed: "))
b_angl = float(input("Input starting angle: "))
vy = b_h * sin(b_angl)
vx = b_h * cos(b_angl)
g = 9.8
t = (vy/8)
Max_height = (vy - (-g*t*t)) # max height of the throw
tx = 0
Projectile_CS = (vy * tx) - (-g*tx*tx) # current spot of the projectile
while Projectile_CS >= 0:
tx += 0.1
else:
print("Done")
Max_distance = tx * vx
print("Max")
for x in range(round(float(Max_height)/5)):
print("*")
print("0", "_" * t, "_" * round((Max_distance)/5))
print("The maximum height is: ", round(Max_height))
print("Max distance is: ", round(Max_distance))
return
Sling()
After the while Projectile_CS >= 0: part of the code, it doesn't do anything, or looks like it. I can stop the code or restart with debugging but it doesn't help.
You never update Projectile_cs in the while loop, therefore you get stuck inside it
Your while loop continues until Projectile_speed value is < 0, but since you never change projectile_speed value in the while loop it will forever be < 0
For your code to work you have to update your variable in the while loop, according to what you did earlier it would probably look something like that
Projectile_CS = (vy * tx) - (-g*tx*tx)
while Projectile_CS >= 0:
tx += 0.1
Projectile_CS = (vy * tx) - (-g*tx*tx)

What is wrong with my code when calculating the quadratic equation? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
print("Program to find roots of a quadratic equation.")
a = float(input("Enter 1st coefficient of equation(a) : "))
b = float(input("Enter 2nd coefficient of equation(b) : "))
c = float(input("Enter 3rd coefficient of equation(c) : "))
d = (b**2)-(4*(a*c))
if d > 0 or d < 0:
d = d**(1/2)
x1 = ((-b + d) / 2) <- problem 1
x2 = ((-b + d) / 2) <- problem 2
print("Coefficients of equation : ",x1,"and",x2)
else:
x = (-b/(2*a)) <- unnecessary
print("Coefficients of equation : ",x)
I am getting this as output:
Program to find roots of a quadratic equation.
Enter 1st coefficient of equation(a) : 5
Enter 2nd coefficient of equation(b) : 5
Enter 3rd coefficient of equation(c) : 5
Coefficients of equation : (-2.4999999999999996+4.330127018922194j) and (-2.4999999999999996+4.330127018922194j)
Process finished with exit code 0
]
solve for the discriminate. notice your x1 and x2 have the same equation. this is the quadric function. I corrected the formula.
import math
a=100
b=200
c=10
# calculate the discriminant
d = (b**2) - (4*a*c)
# find two solutions
x1 = (-b-math.sqrt(d))/(2*a)
x2 = (-b+math.sqrt(d))/(2*a)
print("Coefficients of equation {x1} and {x2}".format(x1=round(x1,2),x2=round(x2,2)))

Euclid's division algorithm (to find HCF ) is there a better way? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#HCF
#input
C = int(input("the bigger number:" ))
D = int(input("the smaller number:" ))
#division
N = "="
M = "x"
A = "+"
#i don't know if I can add this to the while loop
Q = C//D
S = C%D
print (C,N,D,M,Q,A,S)
E = S
s = D
D = C
#Euclid's division algorithm
while S != 0:
Q = s//E
S = s%E
print(s, N,E, M, Q, A, S)
s = E
if S == 0:
print ("HCF =",E)
else :
E = S
is there a better way of writing this ?
if there is a syntax I am using incorrectly pls tell.
I don't know why I can't post this it's showing your post is mostly code pls explain ignore this last part it's only so this problem goes away.
Here's another way of writing your code
Corrects an error in posted code which provides incorrect results
Uses variables from an algorithm to provide a reference for variable names
Uses string interpolation to show formula
Euclidean Algorithm
Reference
Implementation
a, b, q, r corresponds to a, b, qi, ri in the algorithm
def euclidean_algorithm(a, b):
" Displays iterations of the euclidean algorithm "
if b > a:
# swap if necessary
# to ensure a is the larger number
a, b = b, a
while b:
q = a // b
r = a - q*b
print(f'{a} = {q} x {b} + {r}') # use string interpolation to show formula
a, b = b, r
print(f'HCF = {a}')
return a
Usage
a = int(input('First number: '))
b = int(input('Second number: '))
euclidean_algorithm(a, b)
Output
First number: 498
Second number: 222
498 = 2 x 222 + 54
222 = 4 x 54 + 6
54 = 9 x 6 + 0
HCF = 6

is there possible to resolve this star pyramid [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)
I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)
just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))
n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.

How to make a binomial expander in python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example
(x + y)^4 = x^4 +(4x^3)y + (6x^2)y^2 + + 4xy^3 + y^4
I'm using python 3.3.2 and I don't know where to start, I need a little guidance. I'm not asking for the answer to it, just the general steps to make this program work. I've done a few other programs before, and this is probably pushing the limit on what I can do.
SymPy already do that for you:
>>> import sympy
>>> x, y = sympy.symbols("x y")
>>> formula = (x + y) ** 4
>>> formula
(x + y)**4
>>> formula.expand()
x**4 + 4*x**3*y + 6*x**2*y**2 + 4*x*y**3 + y**4
If you need a visualization as a string with "^", you can do:
>>> str(formula.expand()).replace("**", "^")
'x^4 + 4*x^3*y + 6*x^2*y^2 + 4*x*y^3 + y^4'
The code below can be found on this site.
Usage
python binomialexpansion.py 3
Output
x^3 + 3x^2y + 3xy^2 + y^3
Code
import sys
power = int(eval(sys.argv[1]))
strpower = str(power)
coeffs = []
if power == 0:
print 1
exit()
if (power+1) % 2 == 0:
turningp = (power+1)/2
counter = 1
else:
turningp = (power+2)/2
counter = 2
for i in range(1, power+2):
if i == 1:
sys.stdout.write("x^"+strpower+" + ")
coeffs.append(1)
continue
if i == power+1:
print "y^"+strpower,
coeffs.append(1)
break
if i > turningp:
co = coeffs[turningp-counter]
counter = counter+1
else:
co = ((power-(i-2))*coeffs[i-2])/(i-1)
coeffs.append(co)
sys.stdout.write(str(co))
if power-(i-1) == 1:
sys.stdout.write("x")
else:
sys.stdout.write("x^"+str(power-(i-1)))
if i-1 == 1:
sys.stdout.write("y ")
else:
sys.stdout.write("y^"+str(i-1)+" ")
sys.stdout.write("+ ")

Categories

Resources