3

I am very new to programming and ran into a problem.

Traceback (most recent call last):
  File "C:/Users/aecas/AppData/Local/Programs/Python/Python38-32/Project.py", line 24, in <module>
    finalpay = usercommission + userpay
TypeError: unsupported operand type(s) for +: 'float' and 'str'

The program that I wrote is:

#Prompt user for name
username = input("What is your employee name?")

#Prompt user to enter number of hours worked
userhours = input("Please enter number of hours worked this week: ")


#Prompt user for weekly sales
usersales = input("Enter weekly sales amount:")

commission = float(usersales)

#calculate the commission (.2*user sales input)
usercommission = (20/100) * commission

#hourly rate which I establish
rate = 15

#User pay before commmission
userpay=  (rate * userhours)

#Compute total pay for user
finalpay = usercommission + userpay

print(name, "Your total pay is: ",finalpay)
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • 1
    `input()` returns string values. Therefore `userhours` is a string, and `userhours * 15` is that string repeated fifteen times. – John Gordon Oct 07 '20 at 02:01
  • Does this answer your question? [Cannot concatenate 'str' and 'float' objects?](https://stackoverflow.com/questions/16948256/cannot-concatenate-str-and-float-objects) – Aayush Panda Oct 07 '20 at 02:04

3 Answers3

0

You need to convert userhours to an integer. So do userhours = int(userhours).

user2233706
  • 6,148
  • 5
  • 44
  • 86
0

It is easy to understand your problem, it stated "TypeError: unsupported operand type(s) for +: 'float' and 'str'", which means your first value is 'float', your second is 'string'

When looking your code, you convert the first input to float, but not the userhours. When you multiply a string with a number like: userpay= (rate * userhours). Your string userhours will rurn into duplicate string at userpay. Therefore, convert userhours first. And then you can add.

dtlam26
  • 1,410
  • 11
  • 19
0

input is returning a string, not a float. usersales gets cast to a float(), but userhours never gets cast.

Also, in the last line, name would be undefined, think it needs to be username

Try this:

#!/usr/bin/python3
#Prompt user for name
username = input("What is your employee name?")

#Prompt user to enter number of hours worked
userhours = float(input("Please enter number of hours worked this week: "))


#Prompt user for weekly sales
usersales = input("Enter weekly sales amount:")

commission = float(usersales)

#calculate the commission (.2*user sales input)
usercommission = (20/100) * commission

#hourly rate which I establish
rate = 15

#User pay before commmission
userpay=  (rate * userhours)

#Compute total pay for user
finalpay = usercommission + userpay


print(username, "Your total pay is: ",finalpay)
NateB
  • 509
  • 1
  • 3
  • 9
  • @alfredocastro - Glad I could help. If this answered your question, please consider upvoting/accepting the answer, thanks – NateB Oct 07 '20 at 13:38