简体   繁体   中英

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

Ok so i am writing a program to find the day of the week and the program works smoothly until this block

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 )

the error is TypeError: unsupported operand type(s) for -: 'int' and 'str'

When you put inverted commas round something it makes it into a string, so 'month' means the word this, whereas month means the value in the variable called month.

Your program will stop giving you that particular error if you remove the ' s:

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 )

Had you defined the values of month etc before?

Use variables by their name directly, without '' . Words in '' indicates a string, not a variable.

For example:

A = 1
print(A,'A')

It will output 1, A

For your case, the code should be modified as:

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 did define the variables you used.

There are a number of things wrong with this code sample, and the error is only going to be the first of many, as you work through fixing the problems.

'month' seems like it should be a variable of some sort, and not a string. The actual error is occuring on that line:

A = ( (14 - 'month') /12)

You are taking the magic number 14 and subtracting a string from it. You simply can't do that. You can't subtract strings from integers. Surrounding month in single quotes makes it a literal string. As AndrewC mentions, if you remove the single quotes from month it becomes a variable, but it will need to have a value before you can perform an operation in it, like subtraction.

If you can add some context as to what you are trying to do here, it might make it easier for us to help you.

This type of error would be caused by something along these lines:

"some string" + anInt - anotherInt

The problem arises because of the String in this statement- the compiler interprets the plus sign as combining the String and int together. However, in this context, it doesn't know what to do with a minus sign- you can't subtract an int from a string.

Your problem can be solved by putting your integer operations inside sets of parenthesis- ie,

"some string" + (anInt - anotherInt)

If you're still having trouble, we can review your exact code and see where these parenthesis should be added- but this may be enough to get by on your own, which is always preferable!


EDIT: I'll leave the above post in case it is part of the issue as well, but after reviewing the code again, you have this line of code:

A = ( (14 - 'month') /12)

In which you're subtracting the String 'month' from the int 14. That'd probably be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM