Tip Calculator in Python [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
def solve(meal_cost, tip_percent, tax_percent):
tip_percent=float(meal_cost * (tip_percent / 100))
tax_percent=float(meal_cost * (tax_percent / 100))
total_cost=meal_cost + tip_percent + tax_percent
return (total_cost)
print(solve(12, 20, 8))
The answer should be 15, but the above is giving 12.0. Not sure why.
when I tried printing tip_percent, it's giving 0.0.
tip_percent calculated as follows:
float(meal_cost * (tip_percent / 100))
I'm learning Python at the beginning stage.

Your results are consistent with using Python 2. In Python 2 / is integer division if the numbers to divide are integers. To force float math change 100 to 100.0:
def solve(meal_cost, tip_percent, tax_percent):
tip_percent=float(meal_cost * (tip_percent / 100.0))
tax_percent=float(meal_cost * (tax_percent / 100.0))
total_cost=meal_cost+tip_percent+tax_percent
return total_cost
print(solve(12,20,8))
Or change to using Python 3.
Results:
15.36

That's because you are testing with Python 2, which returns 12.0.
In Python 3 the same code would return 15.36.
The reason is Python changed how they handle division in Python 3.
For example:
3 / 2 = 1 (in Python 2.7.6)
3 / 2 = 1.5 (in Python 3.4.1)
To fix that divide by 100.0 (not by 100).
Exampels you could find here: https://riptutorial.com/python/example/2797/integer-division

Related

Why is this code so much faster in Python 2 than Python 3? [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 4 days ago.
The community is reviewing whether to reopen this question as of 4 days ago.
Improve this question
This is an algorithm for calculating the least number of perfect squares, that, when added, equal n
def numSquares(n):
square_nums = [i**2 for i in range(0, int(math.sqrt(n))+1)]
dp = [float('inf')] * (n+1)
dp[0] = 0
for i in range(1, n+1):
for square in square_nums:
if i < square:
break
dp[i] = min(dp[i], dp[i-square] + 1)
return dp[-1]
It runs pretty fast in Python 2, but horrendously slow in Python 3. Anyone know why?

problem with simple calculating overtime pay on python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
My Question is:
Write a function that expects both a person's weekly hours worked (a float value) and their regular hourly wage (a float value). Return a person's total wage for the week (a float value), taking into account overtime.
Any hour worked beyond the 45th hour is considered overtime and is paid at 1.4 times the regular hourly rate. However, if a person works more than 50 hours, they are paid double the hourly rate. Thus, for a maximum of 10 hours of overtime, they will receive 1.4 times the normal rate of pay and thereafter double (2.0 times) the normal rate of pay.
def overtime(working_hours, hourly_rate):
if 50>= working_hours >= 40:
return 40 * hourly_rate + ((working_hours % 40) * 1,4)
elif 40>= working_hours:
return working_hours * hourly_rate
else:
return (40 * hourly_rate) + ((working_hours % 40) * (1,4 * hourly_rate)) + ((working_hours % 50) * (hourly_rate * 2))
TypeError: can't multiply sequence by non-int of type 'float'
Can someone help me? :)
I think you just need to replace , with . in your numbers i.e 1.4 and not 1,4

Calculating Percentage in Python [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to calculate percentage in Python, by hand this works fine but in python this formula for some reason does not work. I tried multiple ways but just not getting correct results.
getPercent = lambda part, whole: float(1 - (float(part)/float(part))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
Output:
0.0
output should be:
0.95
Just use whole instead of part in the denominator.
getPercent = lambda part, whole: float(1 - (float(part)/float(whole))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
Try it online!
you haven't used whole in your lambda function
getPercent = lambda part, whole: float(1 - (float(part)/float(whole))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
# output 0.9489222484855064
You need to divide by whole. To get just two decimal places:
get_percent = lambda part, whole: float(1 - part / whole) * 100
val_1 = float(3194.15)
val_2 = float(3163.84)
a_float = get_percent(val_2, val_1)
print("{:.2f}".format(a_float))
Returning:
0.95

How to Send Input as a List Into Function? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I trying to pass the input to the function but my code has a problem.
I have position with the (x, y) coordinates for each points.
I would be appreciate if someone help me to fix my program.
def distance(ball1,ball2):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(float(d))
ball1=[2,10]
ball2=[3,5]
distance(ball1,ball2)
Error has been fixed, But the output is not accurate and the result always is 1.
May you please help to fix that too? thanks
The issue is that you are accessing the third element in ball2, with ball2[2]. However, there are only two elements in the list. You need to change it to ball2[1]-ball1[1]:
def distance(ball1=[],ball2=[]):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(d)
You cannot access the third element.
Code:
def distance(ball1=[],ball2=[]):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(d)
ball1=[1,1]
ball2=[3,3]
distance(ball1,ball2)
def distance(ball1=[], ball2=[]):
d = (((ball2[0] - ball1[0]) ** 2) + ((ball2[1] - ball2[1]) ** 2)) ** (1 / 2)
print(d)
ball1 = [1, 1]
ball2 = [3, 3]
distance(ball1, ball2)
The 1 result is due to division.
In Python 2 1/2 does integer division with 0 result.
In Python 3 1/2 works computes to 0.5
Just replace 1/2 with 0.5 in your code.

Precise Python 2.7 calculation including Pi [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I am working on a small program that calculates different things related to orbital mechanics, such as the semi-major axis of an orbit, velocity etc. (No experience in this subject is required to answer this).
Everything worked out well, until I tried to calculate the orbital period (a formula where I had to use Pi):
T = 2π √ a3 / µ (Click for image)
Where T is time in seconds, a is the semi-major axis, and µ is the standard gravitational parameter.
Now, my problem is that my program does not calculate it very precise, as the result of the formula is a bit off; for example: a circular orbit at an altitude of 160km should take approx 88 minutes, but my program tells me an approx of 90 minutes and 37 seconds.
My code:
#the standard gravitational parameter of the Earth
gravPara = 3.986004418*10**14
#the semi-major axis of the orbit (the radius of the Earth + the apoapsis and periapsis of your orbit / 2)
semiMajor = (bodyDia + ap + pe) / 2
#formula to calculate orbital period
timeSeconds = (2 * math.pi) * math.sqrt(semiMajor**3 / gravPara)
#making the result appear as minutes and seconds instead of just seconds
timeMinutes = 0
while (timeSeconds > 60):
timeSeconds = timeSeconds - 60
timeMinutes = timeMinutes + 1
#round the variable to store seconds
round(timeSeconds, 0)
#print the result
print timeMinutes
print timeSeconds
So my question is: is it an error in my code, or is math.pi not very precise when used together in such a formula? Should I store it as a float and use the float instead or should I split up the calculation into multiple pieces?
I would be very thankful if you could help me out on this one, as searching through the Python reference as well as other forums did not get me very far.
PS: when using print math.pi it returns a precise value of Pi, so the math.pi function seems to be working correctly.
math.pi is a float with 15 decimals: 3.141592653589793
As per chepners comment to your original post, that equates to about the size of an atom when calculating spheres the size of the earth.
So to answer your question: it's not math.pi
Okay - seems like I have found the solution to my problem; while editing my question to include my calculation for the variable semiMajor, I realized I forgot to include parentheses around bodyDia + ap + pe, which caused faulty prioritizing, leading to the not-very-precise calculation.
So it was just a silly mistake in my code, and it was easily solved by adding two parentheses.
Thank you for taking your time.

Categories