-9

i am subtracting two variables in python . one variable from input and another one predefined . i am getting error it shows " y=name - name2 TypeError: unsupported operand type(s) for -: 'str' and 'int' "enter image description here

  • 3
    Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`) … [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors-when-asking-a-question) – wwii Jul 16 '22 at 16:11
  • 1
    Welcome to Stack Overflow. Please read [ask] since your first question is not well received here. Your question might be better received if you spellcheck it and use proper punctuation (use capitals at start of a sentence, etc.). Use code formatting for code and error messages to make it more readable. Do NOT post images. And most importantly: first [search](https://stackoverflow.com/search?q=%22TypeError%3A+unsupported+operand+type%28s%29+for+-%3A+%27str%27+and+%27int%27%22) for an answer before posting a new question. This question has been asked and answered many many times already. – wovano Jul 18 '22 at 18:22

2 Answers2

2

input function in python returns string by default, so if you want to use mathematical operations, you have to converts that string to int or float.

following code should work

import time

number1 = int(input("Enter Your Number: ")
number2 = 5000

y = number1 - number2

print("Your age is ", y)
1

You have to cast(convert) the input value to int.

This your code according to the image


import time

name = input("Enter your number:" )

names = 5000

y = name - name2

print("your age is ", y)

Change it to this


name = int(input("Enter your number:" ))

names = 5000

y = name - name2

print("your age is ", y)

Maxwell D. Dorliea
  • 1,086
  • 1
  • 8
  • 20