为啥我收到 TypeError: unsupported operand type(s) for -: 'str' and 'int'
Posted
技术标签:
【中文标题】为啥我收到 TypeError: unsupported operand type(s) for -: \'str\' and \'int\'【英文标题】:why am I getting the TypeError: unsupported operand type(s) for -: 'str' and 'int'为什么我收到 TypeError: unsupported operand type(s) for -: 'str' and 'int' 【发布时间】:2021-03-27 11:06:00 【问题描述】:我正在尝试制作一个打印日历的程序,但它也没有打印我想要的内容。
假设打印:
Month
Sun Mon Tues Wed Thurs Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29
而是打印:
Month
Sun Mon Tues Wed Thurs Fri Sat
for x in range(start-1)] + [str(x) for x in range(1,days+1)] + [ for x in range(start-1)] + [str(x) for x in range(1,days+1)] + [ for x in range(start-1)] + [str(x) for x in range(1,days+1)] + [ for x in range(start-1)] + [str(x) for x in range(1,days+1)] + [ for x in range(start-1)] + [str(x) for x in range(1,days+1)] + [
这就是我所拥有的,有人可以帮忙解决问题吗?
while (month != "January" and month != "February" and month != "March" and month != "April" and month != "May" and month != "June" and month != "July" and month != "August" and month != "September" and month != "October" and month != "November" and month != "December"):
print ("Thats not a correct response, please try again. ")
month = input("Enter the name of the month: ")
week = input("Enter the day of the week the month begins (1-7): ")
while (week != "1" and week != "2" and week != "3" and week != "4" and week != "5" and week != "6" and week != "7"):
print ("Thats not a correct response, please try again. ")
week = input("Enter the day of the week the month begins (1-7): ")
numDays = input("Enter the number of days in the month: ")
while (numDays != "28" and numDays != "29" and numDays != "30" and numDays != "31"):
print ("Thats not a correct response, please try again. ")
numDays = input("Enter the number of days in the month: ")
name = month
days = numDays
start = week
c = [" for x in range(start-1)] + [str(x) for x in range(1,days+1)] + [" for x in range(35-(days+start-1))]
print(":^50s".format(name))
print("\t".join(["Sun","Mon","Tues","Wed","Thurs","Fri","Sat"]))
for x in range(0,len(c),7):
print("\t".join(c[x:x+7]))
【问题讨论】:
如果这是一个直接的副本,那么您在声明c
时会遇到语法错误。有多余的双引号。
那我该如何解决
如何修复它首先取决于您到底想要什么(或您想要做什么)。你希望你的第一个列表理解 ([" for x in range(start-1)]
) 给你什么? c
最终应该是什么?
【参考方案1】:
您需要在此处修复线路:
c = [" for x in range(start-1)] + [str(x) for x in range(1,days+1)]...
Python 正在寻找一个字符串,因为您使用了"
。删除它们以及它们之后/之前的空间,您的代码应该可以工作!
编辑:
该行应如下所示:
c = [for x in range(start-1)] + [str(x) for x in range(1,days+1)] + [for x in range(35-(days+start-1))]
【讨论】:
但是它说语法无效以上是关于为啥我收到 TypeError: unsupported operand type(s) for -: 'str' and 'int'的主要内容,如果未能解决你的问题,请参考以下文章
为啥我收到 TypeError: Failed to fetch?
为啥我收到 TypeError: unsupported operand type(s) for -: 'str' and 'int'
为啥我收到“TypeError:this.state.users.map is not a function”,用于部署的 React 应用程序的生产版本
为啥我会收到“TypeError: unsupported operand type(s) for -: 'str' and 'str'”错误?