I had this code that iterates through users input and returns the output
while True:
city = input("Enter city.")
if city in city_list:
print (city,'selected')
break
else:
print ('Please try again.')
As I am asking several times for input, it looked redundant, I tried to capsulate it in one function, here is the entire function
def get_filters():
print ('Welcome .')
#list for month and day data_user
city_list=['city1','city2','city3']
month_list=['January', 'February', 'March', 'April', 'May','June','All']
day_list=['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday','All']
city_string=('Enter city.')
city=None
month_string=('Enter month.')
month=None
day_string=('Enter day.')
day=None
def check_input(data, s_string, data_list):
while True:
data = input(s_string)
if data in data_list:
print (data,'selected')
return data
break
else:
print ('Please try again.')
check_input(city, city_string, city_list)
check_input(month, month_string, month_list)
check_input(day, day_string, day_list)
return city, month, day
If I run the code I get an error message that later in the code the values can't be loaded.city, day and month are not getting passed. Is there something wrong in how I return the values?
check_input: you need to do:city = check_input(city,city_string,city_list). Yourcityinsidecheck_inputis not in the scope of your higher function, so for the higher function,cityis still atNone