problem with simple calculating overtime pay on 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 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

Related

I'm getting a ''Expected ":" Pylance error and i dont know what i did wrong [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 last month.
Improve this question
This is the code. I know that the "question" variables are useless, but that's how i like to do it.
I was supposed to make a pay computation program that asks for input about hours worked and rate of pay and pay 1.5 times the rate for hours worked above 40.
question = ('How many hours have you worked this month ?\n')
question1 = ("What's the rate of pay ?\n")
hrs = input(question)
rate = input(question1)
if hrs > 40 :
pay = (hrs-40) * (float(rate) * 1.5)
print ('Your payment is ', pay + 'dollars.')
else hrs < 40 :
pay = hrs*rate
print ('Your payment is ', pay + 'dollars.')
I tried to nest it a different way and googled python debugger, but nothing helped. Please tell me how to fix this error and where my mistake is so that i can learn. Thank you !
Hope following code will help you.
question = ('How many hours have you worked this month ?\n')
question1 = ("What's the rate of pay ?\n")
hrs = input(question)
rate = input(question1)
hrs = int(hrs)
rate = int(rate)
if hrs > 40 :
pay = (hrs-40) * (float(rate) * 1.5)
print ('Your payment is ', str(pay) + 'dollars.')
elif hrs < 40:
pay = hrs*rate
print ('Your payment is ', str(pay) + 'dollars.')
keep in mind that, input() will always return string datatype, and you have to convert into the int for arithmatic operation,
Wherenver you try to concat string, then you have to convert int to string

i dont know where the error is in my code [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'm new to python and cant find where my error is. Python is saying that there is a syntax error but it wont tell me where it is. My task is to make a programme that can count a taxi fare. Here is my code.
distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")
cost = float(distance) + 3 - 1 * 2
if passengers == 5:
cost = cost * 2
print("The total cost of the journey is £" cost)
else:
print("The total cost of the journey is £" cost)
The error is probably something simple but I can't find it. Thanks in advance.
if you want to print several things with print() function, you need to separate them with commas
try this:
print("The total cost of the journey is £", cost)
You're not concatenating your strings, if you're on python 3.6 or higher use f-strings:
distance = input("Enter the distance of the taxi ride in kilometers (km): ")
passengers = input("Enter the amount of passengers from 1 to 5: ")
cost = float(distance) + 3 - 1 * 2
if passengers == 5:
cost = cost * 2
print(f"The total cost of the journey is £ {cost}")
else:
print(f"The total cost of the journey is £ {cost}")

Tip Calculator 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 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

Python 2.7 unknown syntax error [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 6 years ago.
Improve this question
Here is my code
hrs = raw_input("Enter Hours:")
h = float(hrs)
rate = raw_input("Enter Rate:")
r = float(rate)
if hrs <= 40
pay = hrs * rate
print pay
else hrs > 40
pay = hrs * 15.75
print pay
Here is the error message
486406789.415.py", line 6
if hrs <= 40
^
SyntaxError: invalid syntax
You are missing colons (:) after the conditions. Also, note that else doesn't take a condition, you need to use elif:
if hrs <= 40:
# Here -^
pay = hrs * rate
print pay
elif hrs > 40: # Note the elif
# Here --^
pay = hrs * 15.75
print pay
There are multiple errors in your code. The invalid syntax error is quite easy to fix, just add the colon in the end of the if statement and the else does not take any conditions (elif, however would). This is very basic Python syntax. You can always check the official Python tutorials and documentations as a first step to troubleshoot syntax errors. e.g.:
http://www.tutorialspoint.com/python/python_if_else.htm
after you fix the SyntaxError you will run into other issues when comparing strings with integers, as in hrs <= 40. Instead you want to compare the your converted input h. The same goes for the calculations: use h instead of hrs and r instead of rate.
See if you can solve all the issues. If you don't manage, here is a working example of the code: https://gist.github.com/fabianegli/bae9864e5166fac4dd2baeccd5ed3f8d

Check if sum of two amounts is equal to a certain total with some tolerance [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
How can I check if the result of multiplying two amounts is equal to a certain total give or take a few cents. For example: 5.57 * 2.92 = 16.2644 and 3.25 * 5 = 16.25.
I am increasing the first amount which is the stake by 0.01 each time to try find the closest amount to the total, the second amount does not change.
If you're making financial-type calculations in Python (or any programming language), you do not want to use floating point numbers (http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems).
Instead, be sure to at least use the decimal module, which will give you arbitrary-precision decimal numbers (http://docs.python.org/2/library/decimal.html).
As for your actual problem:
from decimal import Decimal
r1 = Decimal("5.57") * Decimal("2.92")
r2 = Decimal("3.25") * Decimal("5")
epsilon = Decimal("0.01")
if abs(r1 - r2) <= epsilon:
print "Almost equal!"
decimal is good.
But to compare two floats within a tolerance:
tolerance = 0.04
if abs(number1 - number2) < tolerance:
print('the numbers are close')

Categories