【问题标题】:NameError: name 'Jan' is not definedNameError:名称“Jan”未定义
【发布时间】:2020-07-05 20:39:45
【问题描述】:

当我尝试运行代码时,我不断收到同样的错误消息 NameError: name 'Jan' is not defined 有人可以告诉我如何解决这个问题

def main():
    # imports ability to read text files
    import csv
times=[1,273,93,59,266,128,2,41,110,315,43,263,3,78,649,620,538,502,4,456,648,618,537,501,5,238,156,160,310,127,6,768,647,616,534,459,7,6785,646,614,533,458,8,567,645,613,531,457,9,700,644,612,530,457,10,678,643,610,529,456,11,78,642,609,527,455,12,659,641,608,526,454,13,6543,640,606,525,453,14,567,639,605, 524,453,15,533,638,604,522,452,16,246,637,602,521,451,17,5,636,601,520,450,18,90,635,600,519,450,19,7,634,558,517,449]
    dates = [Jan,Feb,March,April,May]
     
    raceTimes = (input("PLease enter a time you would like to search for:"))
    found = False
    
    # open text file
    with open("sun_data.csv") as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        
        # reads the text file
        for row in readCSV:
        # comparing values in text file with the user-entered time
              found = True
        for i in range(len(years)):
            if years[i] == x:
              found = True
            print(str(x) + " was found at position " + str(i))
            break
        if not found:
            print("I'm sorry, there is no race scheduled at that time.")
    # Close input file
    csvfile.close()
    
main()

【问题讨论】:

  • Jan 是一个名为 Jan 的变量。"Jan"(引用)是一个字符串。
  • 这是否意味着我需要在 Jan 或括号中添加引号

标签: python nameerror


【解决方案1】:

您可能打算将日期存储为字符串?

如果是这样改变

dates = [Jan,Feb,March,April,May]

dates = ['Jan','Feb','March','April','May']

【讨论】:

    【解决方案2】:

    你得到NameError 因为Jan 确实没有定义。我相信您希望月份成为字符串:

    dates = ['Jan','Feb','March','April','May']
    

    【讨论】:

      【解决方案3】:

      我猜你尝试输入数组“日期”的项目。看起来项目应该是需要将它们设置在引号中的字符串。

      日期 = ["一月","二月","三月","四月","五月"]

      https://www.w3schools.com/python/python_arrays.asp

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-24
        • 1970-01-01
        • 2021-04-15
        • 2019-01-26
        • 2021-10-05
        • 2017-08-16
        • 2019-08-18
        相关资源
        最近更新 更多