在python的if语句中分配变量

时间:2020-08-29 11:50:42

标签: python pandas

我是python的新手,正在尝试根据一些用户输入来导入数据。用户有两个选项,是否过滤数据。根据输入,我想设置两个变量月和日。这些将在另一个功能中使用。

下面的代码返回一个UnboundLocalError:赋值之前引用的本地变量'month'

如果用户输入的值不正确但未包含在下面,我会提出一些方案。

def get_filters():
    filter_response = ['time','none']
    filter = input('Would you like to filter by time? Type "none" to view all data.\n').lower()
        
    if filter_response=='time':
        month_response = ['jan','feb','mar','apr','may','jun']
        month = int(input('Which month would you like to view data for?))
        day_response = [sun,mon,tue,wed,thu,fri,sat,sun]
        day = int(input('Which day would you like to view data for?))

    else:
        month == 'all'
        day == 'all'

def load_data(month, day):
'''
load data and apply filter if required
'''
return df  

1 个答案:

答案 0 :(得分:1)

我已更改month == 'all'语句,因为==运算符在2个值之间进行比较,并且不分配值。在这里,您将month'all'和{{1 }}在当前作用域中不存在,因此会出现错误。

month
相关问题