【问题标题】:"NameError: name 'day_time' is not defined" error in Python3.8 [duplicate]Python3.8中的“NameError:名称'day_time'未定义”错误[重复]
【发布时间】:2020-07-28 03:56:58
【问题描述】:

我正在制作一个程序,它可以根据输入的时间告诉一天中的哪个部分。

我的代码:

user_day = input("What's the time? ")

if user_day >= 20 and user_day <= 24:
    day_time = "Night"

elif user_day >= 24 and user_day <= 12:
    day_time = "Morning"

elif user_day >= 12 and user_day >= 17:
    day_time = "Noon"

elif user_day >= 17 and user_day >= 20:
    day_time = "Evening"

但我收到此错误:

if day_time == 1 and user_weather == plus:
NameError: name 'day_time' is not defined

请帮帮我。

【问题讨论】:

  • 这个错误代码相对于代码的if user_day... 部分在哪里?

标签: python python-3.x if-statement nameerror


【解决方案1】:

如果你想稍后使用它,你需要在 if 块的上下文之外声明day_time

像这样,例如:

user_day = input("What's the time? ")

day_time = None

if user_day >= 20 and user_day <= 24:
    day_time = "Night"

elif user_day >= 24 and user_day <= 12:
    day_time = "Morning"

elif user_day >= 12 and user_day >= 17:
    day_time = "Noon"

elif user_day >= 17 and user_day >= 20:
    day_time = "Evening"

【讨论】:

    【解决方案2】:
    • 问题是,user_day 在作为输入输入时是一个字符串。
      • 因为它是一个字符串,它不满足任何条件,因此,day_time 保持未定义。
        • 将输入包装在try-except 块中以检查输入是否为正确的数据类型。在这种情况下,str 类型可以转换为intfloat
      • 必须将其转换为 intfloat,具体取决于您是仅接受小时数还是小数小时数。
    • user_day 应该在 while True 循环中继续请求时间,直到收到有效的数字输入。
    • 最后,对于这种情况,通过正确地从小到大对条件进行排序,可以更有效地编写代码。
      • user_day 将逐渐下降到正确的条件。
    while True:
        try:
            user_day = int(input("What's the hour on a 24-hour scale? "))
        except ValueError:  # checks for the correct data type; maybe someone will spell the hour
            print('Please enter the hour as a numeric value')
        
        if (user_day >= 24) or (user_day < 0):  # 24 and over or less than 0 are invalid times
            print('Please enter a valid time')
        else:
            break  # break when there's valid input
    
    if user_day < 12:
        day_time = 'Morning'
    elif user_day < 17:
        day_time = 'Noon'
    elif user_day < 20:
        day_time = 'Evening'
    else:
        day_time = 'Night'
        
    print(day_time)
    

    使用np.digitize

    • 返回每个输入值所属的 bin 的索引。
    • 使用np.digitize返回的值来索引day_time中的正确值
    import numpy as np
    
    while True:
        try:
            user_day = int(input("What's the hour on a 24-hour scale? "))
        except ValueError:  # checks for the correct data type; maybe someone will spell the hour
            print('Please enter the hour as a numeric value')
        
        if (user_day >= 24) or (user_day < 0):  # 24 and over or less than 0 are invalid times
            print('Please enter a valid time')
        else:
            break  # break when there's valid input
    
    day_time = ['Morning', 'Evening', 'Noon', 'Night']
    idx = np.digitize(user_day, bins=[12, 17, 20])
        
    print(day_time[idx])
    

    【讨论】:

      【解决方案3】:

      您需要在else 语句中定义day_time,这样当现有条件都不满足时,day_time 仍然有一个值。

      此外,您需要将用户的输入转换为整数,然后才能在其上使用带有字符串的&lt;&gt; 等运算符:

      user_day = int(input("What's the time? "))
      
      if user_day >= 20 and user_day <= 24:
          day_time = "Night"
      
      elif user_day >= 24 and user_day <= 12:
          day_time = "Morning"
      
      elif user_day >= 12 and user_day >= 17:
          day_time = "Noon"
      
      elif user_day >= 17 and user_day >= 20:
          day_time = "Evening"
      
      else:
          day_time = "Unknown"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-01
        • 2020-06-17
        • 2016-07-06
        • 2015-10-22
        • 2016-05-12
        • 2011-11-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多