user2458048
user2458048

Reputation: 114

how to set spaces in strings

I am trying to have the program check the user input for "get up" or "rise and shine" then have it print "im up". The problem is it won't print "im up" it goes straight to the else statement. This code i have right now makes it where if i were to change "get up" to "hello" then if i were to enter anything in the input and included "hello" in the input it will then print "test" and i would like to keep it that way if it is possible? code:

dic = {"get,up", "rise,and,shine"}
test = raw_input("test: ")
tokens = test.split()
if dic.intersection(tokens):
    print "test"
else:
    print "?" 

help is appreciated.

Upvotes: 0

Views: 54

Answers (1)

Blender
Blender

Reputation: 298156

dic.intersection() returns the intersection of two sets. For example:

{1, 2, 3}.intersection({2, 3, 4})  # {2, 3}

You probably just want to test for membership instead:

if tokens in dic:
    ...

Although that won't work either, as you're splitting up the string by spaces, which will make it test for the individual words, not the whole phrase. Also, naming your set dic isn't a good idea. It's a set, not a dictionary.

In short, don't use sets and don't use .split():

phrases = ['get up', 'rise and shine']
phrase = raw_input('Enter a phrase: ')

if phrase in phrases:
    print "test"
else:
    print "?" 

Upvotes: 2

Related Questions