简体   繁体   English

TypeError:-:“ int”和“ str”不支持的操作数类型

[英]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' 错误为TypeError:-:“ int”和“ 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. 当您将逗号放在圆括号中时,它会变成字符串,因此'month'表示单词this,而month表示变量month的值。

Your program will stop giving you that particular error if you remove the ' s: 你的程序将停止给你的特定错误,如果你删除了' 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? 您之前是否定义过month等值?

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 它将输出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. 'month'似乎应该是某种变量,而不是字符串。 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. 您正在使用幻数 14并从中减去一个字符串。 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. 正如AndrewC所提到的,如果您删除月份中的单引号,它将成为一个变量,但是在执行其中的操作(如减法)之前,它需要具有一个值。

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. 出现此问题是由于该语句中的String -编译器将加号解释为将String和int组合在一起。 However, in this context, it doesn't know what to do with a minus sign- you can't subtract an int from a string. 但是,在这种情况下,它不知道如何处理减号-您不能从字符串中减去int。

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. 在其中,您从int 14中减去了String “ month”。这可能是一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' 类型错误:不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; (2) - TypeError: unsupported operand type(s) for /: 'str' and 'int' (2) TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'str' and 'int' TypeError:+ =不支持的操作数类型:“ int”和“ str” - TypeError: unsupported operand type(s) for +=: 'int' and 'str' typeerror 不支持 / &#39;str&#39; 和 &#39;int&#39; 的操作数类型 - typeerror unsupported operand type(s) for / 'str' and 'int' TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError:unsupported operand type(s) for -: 'str' and 'int' + 不支持的操作数类型:“int”和“str”[TypeError] - unsupported operand type(s) for +: 'int' and 'str' [TypeError] TypeError: 不支持的操作数类型 -: 'str' 和 'int' - TypeError: unsupported operand type(s) for -: 'str' and 'int' 类型错误:-= 不支持的操作数类型:'int' 和 'str' - TypeError: unsupported operand type(s) for -=: 'int' and 'str' TypeError:|:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for |: 'int' and 'str'
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM