Reputation: 11
So im trying to finish my code which is to ask the user for their name, hourly pay and hours worked this week and the program has to print the name, hourly rate and how much they earned that week. Also Saturday's pay is time and a half and Sunday's pay is double. Now I have come up with this but when i run the code it says
pay = (days[0] + days[1] + days[2] + days[3] + days[4] + (days[5] * 1.5) + (days[6] * 2)) * wage
TypeError: can't multiply sequence by non-int of type 'float'
This is my code:
days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday']
hours = []
name = input("Enter employee's name: ")
wage = float(input("Enter your hourly rate: "))
for x in range(7):
daily = float(input("Enter the number of hours you worked on " + days[x] +': '))
hours.append(daily)
pay = (days[0] + days[1] + days[2] + days[3] + days[4] + (days[5] * 1.5) + (days[6] * 2)) * wage
print("Employee name: ",name)
print("Hourly rate: ",wage)
print("Total earned this week: ",pay)
Btw this is not my homework, it just a question i saw online!
Upvotes: 0
Views: 47
Reputation: 41
I think you need to use "hours" list instead of days. Your "days" list is the list of all the days and hours is where you are storing all the daily wages and hence it shows typeerror saying you can't multiply float with String.
Replace days with hours in this snippet pay = (hours[0] + hours[1] + hours[2] + hours[3] + hours[4] + (hours[5] * 1.5) + (hours[6] * 2)) * wage
Thanks
Upvotes: 0
Reputation: 407
Just change your pay
declaration to this:
pay = (hours[0] + hours[1] + hours[2] + hours[3] + hours[4] + (hours[5] * 1.5) + (hours[6] * 2)) * wage
Upvotes: 0
Reputation: 10799
days
is a list of strings. days[5]
is the sixth string in that list ("Saturday"
).
In Python, you may "multiply" strings with whole integer numbers.
>>> "Saturday" * 3
'SaturdaySaturdaySaturday'
That number may not be a floating point number:
>>> "Saturday" * 1.5
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
"Saturday" * 1.5
TypeError: can't multiply sequence by non-int of type 'float'
>>>
You probably meant to use hours
rather than days
.
Upvotes: 1