-2

I am trying to do a simple calculation but once the user inputs the weight it wont print out the remaining weight left. comes with the error code

unsupported operand type(s) for -: 'str' and 'int'

here is my code. is there something i am missing?

def Massallowance():
    person = input("Crew or Specialist? ")
    if person== 'Crew':
        definedMass = 100
        weight = input("Please enter weight: ")
        print("Allowance" + weight-definedMass)
    elif person== 'Specialist':
        definedMass = 150
        weight = input("Please enter weight: ")
        print("Allowance" + weight-definedMass)
Saleem Ali
  • 1,363
  • 11
  • 21
vivace
  • 3
  • 3
  • `input()` returns a string. You need to convert it to integer using `int()` function. Also, you need `str(weight-definedMass)` in `print()` as + operator does not work with a string and an integer – kuro Oct 10 '19 at 07:23

3 Answers3

0

input() gives you strings, you can't subtract a string from a number - that doesn't make sense. You should turn the input string into a number with int or float

def Massallowance():
        person = input("Crew or Specialist? ")
        if person== 'Crew':
            definedMass = 100
            weight = int(input("Please enter weight: "))
            print("Allowance" + weight-definedMass)
        elif person== 'Specialist':
           definedMass = 150
           weight = int(input("Please enter weight: "))
           print("Allowance" + weight-definedMass)
rdas
  • 20,604
  • 6
  • 33
  • 46
0

The 'input' method takes a 'String' as an input. So whenever the user inputs a number it is directly converted to a 'string'. Since you cannot substact a string with an integer, you get the error. This should be a simple fix:

def Massallowance():
        person = input("Crew or Specialist? ")
        if person== 'Crew':
            definedMass = 100
            weight = int(input("Please enter weight: "))
            print("Allowance" + weight-definedMass)
        elif person== 'Specialist':
           definedMass = 150
           weight = int(input("Please enter weight: "))
           print("Allowance" + weight-definedMass)
Trollsors
  • 492
  • 4
  • 17
0

You are trying to do minus operation between str and int First convert input string to int and then do operation.

weight = int(input("Please enter weight: "))

Updated code will be like

def Massallowance():
    person = input("Crew or Specialist? ")
    if person== 'Crew':
        definedMass = 100
        weight = int(input("Please enter weight: "))
        print("Allowance" + weight-definedMass)
    elif person== 'Specialist':
        definedMass = 150
        weight = int(input("Please enter weight: "))
        print("Allowance" + weight-definedMass)
Saleem Ali
  • 1,363
  • 11
  • 21