0

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?

2
  • Your indentation is off here, please fix that as well as post the entire error you receive. Commented Apr 30, 2018 at 21:13
  • 2
    You do not save the value reported by your check_input: you need to do: city = check_input(city,city_string,city_list). Your city inside check_input is not in the scope of your higher function, so for the higher function, city is still at None Commented Apr 30, 2018 at 21:16

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.