【问题标题】:TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'TypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”
【发布时间】:2017-08-23 06:16:37
【问题描述】:

我正在尝试制作一个代码,可以打印出带有给定月份和年份的日历。 我的代码如下所示:

#!/usr/bin/python3
"""import calender"""

import calendar
import datetime

""" begin function """


def get_the_first_day_of_the_week(month, year):
    day_of_week = datetime.date(year, month, 1).weekday()
    return


def print_out_calender(months, years):
    this_month = int(months)
    this_year = int(years)
    new = get_the_first_day_of_the_week(this_month, this_year)
    new = int(new)
    calendar.setfirstweekday(new)
    calendar.monthcalendar(this_month, this_year)
    return


print_out_calender(12, 2017)

我希望它打印出日期矩阵,但我收到如下错误: Traceback(最近一次调用最后一次):

  File "./practice12.py", line 25, in <module>
    print_out_calender(12, 2017)
  File "./practice12.py", line 19, in print_out_calender
    new = int(new)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

我是 python 和编码新手,谁能告诉我为什么?

【问题讨论】:

  • 你的意思是 return day_of_week 在 get_the_first_day_of_the_week 里面吗?

标签: python python-3.x


【解决方案1】:
new = get_the_first_day_of_the_week(this_month, this_year)

将get_the_first_day_of_the_week()的返回值赋值给new。

但是,get_the_first_day_of_the_week() 不返回任何内容,即None。

更改函数以返回一些东西(大概是day_of_the_week),它应该可以工作。

【讨论】:

    【解决方案2】:

    那是因为new 在代码中是None。 int 不能接受 None 类型的参数。

    【讨论】:

      【解决方案3】:

      从 get_the_first_day_of_the_week 返回 day_of_week

      【讨论】:

        【解决方案4】:

        您在 get_the_first_day_of_the_week 函数中返回 None。返回 day_of_week 或将 day_of_week 设为全局变量。

        【讨论】:

          猜你喜欢
          • 2019-09-16
          • 2020-08-22
          • 1970-01-01
          • 1970-01-01
          • 2016-12-06
          • 2017-11-14
          • 2021-12-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多