user1337582
user1337582

Reputation: 1

Python dictionaries

I am new to Python and am trying to create a very simplified version of foursquare.

I would like the user to be able to check-in as well as search through their previous check-ins by date and location. The code that I have seems to work alright with the check-in, but the searches are not returning results to the user. I know that my wording in the for loops are off but I am getting confused in regards to how to search through a dictionary.

Many thanks for your help.

print("What would you like to do? \n1 Check-in \n" \
  "2 Search check-ins by date \n3 Search check-ins by location \n4 Exit")

while True:
    import datetime
    current_date = str(datetime.datetime.now())
    check_in = {}
    choice = input("\nYour choice: ")

    if choice == '1':
        location = input("\nPlease enter your current location: ")
        check_in[current_date] = location
        print("You have checked in at", location, current_date)

    elif choice == '2':
        date_search = input("Please enter a date (yyyy/mm/dd) ")
        for date in check_in.keys():
            if date_search in check_in(date):
                print(check_in[key])

    elif choice == '3':
        location_search = input("Please enter a location: ")
        for date in check_in.keys():
            if location_search in check_in(date):
                print(date)

    elif choice == '4':
        break

Upvotes: 0

Views: 225

Answers (2)

PenguinCoder
PenguinCoder

Reputation: 4367

Change your inputs to raw_inputs, else in Python 2.7.2 this code will evaluate the input as code, not an actual number or string. Not exactly what you want, I imagine.

As for your last question of searching through the dictionary, your check for match in a dictionary won't ever match. You reintialize the check_in variable on EVERY loop of the while. You're also inputting the current date/time into the dictionary in a different format than what you're accepting user input for.

Please enter your current location: Home

('You have checked in at', 'Home', '2012-04-16 20:43:08.891334')

Your choice: 2

Please enter a date (yyyy/mm/dd) 2012/04/16

Your choice:

You need to normalize your data and your input, and move the check_in outside of the while loop.

    print("What would you like to do? \n1 Check-in \n2 Search check-ins by date \n3 Search check-ins by location \n4 Exit")


check_in = {}

while(True):
    import datetime
    current_date = datetime.datetime.now().strftime("%Y/%m/%d")
    choice = raw_input("\nYour choice: ")

    if(choice == '1'):
        location = raw_input("\nPlease enter your current location: ")
        check_in[current_date] = location
        print check_in

    elif(choice == '2'):
        date_search = raw_input("Please enter a date (yyyy/mm/dd) ")
        for date,location in check_in.iteritems():
            if date_search == date:
                print "%s - %s" % (date,location)

    elif(choice == '3'):
        location_search = raw_input("Please enter a location: ")
        for date,location in check_in.iteritems():
            if location_search == location:
                print "%s - %s" % (date,location)
    elif(choice == '4'):
        break

Upvotes: 0

Makoto
Makoto

Reputation: 106490

A few remarks:

  1. Every time through the loop, you're flushing check_in. Doesn't matter what we put in there before, it's not going to be around when we come back to it.

  2. Choice 2 can be reduced to a simple d.get(key) statement. You can look up how that works here.

  3. The key you're using doesn't print the way you think it does. If I enter a location, say "Home", the key appears as:

    Please enter your current location: Home
    You have checked in at Home 2012-04-16 19:44:26.235305
    {'2012-04-16 19:44:26.235305': 'Home'} # Print statement added by me.
    

Notice all of that extra information? Yeah, not what you expected. If you want to only get the date portion, you can look more into the datetime module to see how to get it just perfect, or be lazy (like I was while debugging this), and use datetime.datetime.now().split()[0]. This convoluted piece of code splits the string up by spaces, then gets the first element - or '2012-04-16'.

The last choice I'll leave as an exercise to you, but I think that checking the dictionary documentation will be sufficient to get you started.

Upvotes: 1

Related Questions