0

I'm making a user input decision tree and I want to force the dictionary that the input is being compared to into lowercase. I've placed .lower() at various points and keep getting errors.

not_found = True while True: if OPTIONS == "1" or 'a': ARTIST_PICK = str(raw_input( "Please pick an artist\n" "Or Q to quit: ")).lower print ARTIST_PICK **entries = allData(filename).data_to_dict() for d in entries: arts = d['artist']** if ARTIST_PICK in arts: print "found it" elif ARTIST_PICK == 'q': break else: print "Sorry, that artist could not be found. Choose again." not_found = False 

This is a sample of the "entries" I'm trying to make lower and compare the user input to:

[{'album': 'Nikki Nack', 'song': 'Find a New Way', 'datetime': '2014-12-03 09:08:00', 'artist': 'tUnE-yArDs'},] 
2
  • 4
    I see you're doing )).lower right now. Remember to actually call the method. Try )).lower() instead. Also, if OPTIONS == '1' or 'a' doesn't do what you think it does. Commented Dec 11, 2014 at 17:07
  • Missing the () after lower was my typo, sorry. But my problem is how to do I make the d['artist'] lower case also so I can check if the user input is a value in d['artist']? Commented Dec 11, 2014 at 17:26

2 Answers 2

1

If your problem was just comparing the artist names, then you could use list comprehension to make everything lowercase.

entries = allData(filename).data_to_dict() if ARTIST_PICK in [ d['artist'].lower() for d in entries ]: print("found it") elif ARTIST_PICK == "q": break else print("Sorry, that artist could not be found. Choose again.") 

Or if you'd rather use a for loop (rearranged a little for readability):

if(ARTIST_PICK != 'q'): entries = allData(filename).data_to_dict() found = False for d in entries: if ARTIST_PICK == d['artist'].lower(): found = True break elif ARTIST_PICK == "q": break if(found): print("found it") else: print("Sorry, that artist could not be found. Choose again.") else: # handle the case where the input is 'q' here if you want 

By the way, as a matter of principle you should name your boolean variables as though you were using them in a sentence. Instead of setting a variable not_found to False if the variable isn't found, set a variable named found to False or set not_found to True. Makes things easier in the long run.

Sign up to request clarification or add additional context in comments.

2 Comments

The for loop isn't cycling through all the d['artist'] entries, only producing a 'found it' when the first item is entered. Any help there?
Right. Instead of a for loop, this code creates list of all teh artist names in lowercase (the line with the if). I'll add an example with a for loop.
0

ARTIST_PICK = str(raw_input( "Please pick an artist\n" "Or Q to quit: ")).lower()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.