【问题标题】:TypeError: '>' not supported between instances of 'int' and 'str'TypeError:“int”和“str”的实例之间不支持“>”
【发布时间】:2017-04-29 13:54:59
【问题描述】:

我正在编写代码来输入日期(日月和年)以及他们想要重复任务的次数(它的用途。) 我想使用以时间为边界的 for 循环。 代码:

def addtimeslot():
    times = int(times_repeated.get())
    print(times_repeated.get())
    variable_end.get()
    day = variable_day.get()
    month = variable_month.get()
    year = variable_year.get()
    fulldateadd = datetime.date(year, month, day)
    name1 = str(name.get())
    minute = int('00')
    second = int('00')
    hour1 = variable_st.get()
    starttimehour = str(datetime.time(hour1,minute,second))
    hour2 = variable_end.get()
    endtimehour = str(datetime.time(hour2,minute,second))
    for i in range(0 , times):
        fulldateadd = datetime.date(year, month, day)
        cursor.execute( '''INSERT INTO dates (Date, Name, Start, End) VALUES( ?,?,?,? );''', (fulldateadd , name1, starttimehour, endtimehour))
        day = int(day) + 7
        if day > '31':
            month = int(month) + 1

我收到此错误:

TypeError: '>' not supported between instances of 'int' and 'str' 

【问题讨论】:

  • 您已经将dayintday = int(day) + 7 相结合,所以测试应该是if day > 31:,即与int 31 的比较而不是字符串'31'。跨度>
  • 以后请发布完整的堆栈跟踪,这样人们就不必猜测是哪一行导致了错误。
  • 已经解决了,谢谢!
  • 抱歉,我发了才知道。

标签: python python-3.x


【解决方案1】:

由于问题不应该在 cmets 中回答,我将提供@AChampion 的解决评论:

你已经将dayintday = int(day) + 7 制作成day = int(day) + 7,所以测试应该是day > 31:,即与int 31 的比较而不是字符串'31'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2020-11-19
    相关资源
    最近更新 更多