TypeError: unsupported operand type(s) for -: 'int' and 'str' in Python
Hello you guys, I am a newbie in Python and I'm also studying more about Python.
I have some lines of code, I am writing a program to find the day of the week and the program runs smoothly until this block. Function as below:
D = input()
A = ( (14 - 'month') /12)
Y = ( 'Year' - 'A' )
MonthProblem = ( 'month' + 12 * 'A' - 2 )
week = ( ('D' + 'Y' + 'Y'/4 - 'Y'/100 + 'Y'/400 + 31 * 'MonthProblem'/12) % 7 )
But I get an exception TypeError: unsupported operand type(s) for -: 'int' and 'str' when I run code above.
Traceback (most recent call last):
File "main.py", line 1, in <module>
A = ( (14 - 'month') /12)
TypeError: unsupported operand type(s) for -: 'int' and 'str'
And I am using python 3.8.2
Anyone can explain it to me? How can I solve it?
Thanks for any response.
-
P0
Phuong Phan Sep 20 2021
Why do you can a variable wih
''
?Please use variables by their name directly, without
''
. The words in''
indicate a string, not a variable.In your case, the code should be changed as follows :
D = input() A = ( (14 - month) /12) Y = ( Year - A ) MonthProblem = ( month + 12 * A - 2 ) week = ( (D + Y + Y/4 - Y/100 + Y/400 + 31 * MonthProblem/12) % 7 )
if you have defined the variables.
-
U-1
Ubonwan Raksasat Sep 20 2021
I feel you are wrong about way do use a variable.
'D'
is a string value and it is not a variable get from user input. So please change'D'
toD
D = input() A = ( (14 - month) /12) Y = ( Year - A ) MonthProblem = ( month + 12 * A - 2 ) week = ( (D + Y + Y/4 - Y/100 + Y/400 + 31 * MonthProblem/12) % 7 )
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.