繁体   English   中英

为什么我收到 TypeError: unsupported operand type(s) for -: 'str' and 'int'

[英]why am I getting the TypeError: unsupported operand type(s) for -: 'str' and 'int'

我正在尝试制作一个打印日历但它不打印我想要的东西的程序。

假设打印:

              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)] + [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))]

暂无
暂无

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

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