d_wesseling
d_wesseling

Reputation: 141

Python: Values don't get returned from function

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?

Upvotes: 0

Views: 66

Answers (1)

NaN
NaN

Reputation: 26

Not sure what is your intent here, assuming you want to capture the three choices entered by the user. Here is a working code under python3. Please try this...

#!/usr/local/Cellar/python/3.6.5/bin/python3

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)
          return data
          break
      else:
          print ('Please try again.')

selection = (check_input(city, city_string, city_list) , check_input(month, month_string, month_list) , check_input(day, day_string, day_list))

print (selection)


---Output--- 
    Enter city.city1
    city1
    Enter month.January
    January
    Enter day.Monday
    Monday
   ('city1', 'January', 'Monday')

Upvotes: 1

Related Questions