Python: User-specific conversation

1.1k Views Asked by At

I'm new at Python and trying to create a simple - or so I thought - conversation between a bot and a user for a small party I'm hosting in two weeks. However, I would try to make a individual conversation for everybody that will attend. For example: when I type in my name, the script will generate several other questions which are geared to my interests etc., when a friend of mine types in his/her name, the script does the same for him/her. I have the following very short bit of code already, but it does not seem to work. It does work when I ask just one question per user, or if I write out the different questions for one user, but I can't seem to be able to combine both. Could someone help me out. Thanks in advance!

name=raw_input("Hello, what is your name?\n")
if "ohn" in name:
    print "Hello " + name+", glad of you to join us!"\n"
    second_question=raw_input('Why did you come to this party?\n')

elif "arc" in name:
    print "Wow" + name+", so great you are here as well!\n"

closeInput = raw_input("Press ENTER to exit")
print "Closing..."
2

There are 2 best solutions below

0
On

You may also want to consider string formatting instead of concatenating

For instance,

print "Hello {0}, glad of you to join us!\n".format(name)

or

print "Hello %s, glad of you to join us!\n" % name

instead of

print "Hello " + name + ", glad of you to join us!\n"

It may not seem like much, but if you want to start putting more variables into the string you're printing, concatenating the way you're doing can become very cumbersome.

0
On

You put a speech mark in the wrong place on line 3, the code should be:

name=raw_input("Hello, what is your name?\n")
if "ohn" in name:
    print "Hello " + name + ", glad of you to join us!\n"
    second_question = raw_input("Why did you come to this party?\n")

elif "arc" in name:
    print "Wow " + name + ", so great you are here as well!\n"

closeInput = raw_input("Press ENTER to exit")
print "Closing..."

On a side note you should try and add spaces between operators, this will allow you to notice easier what is going wrong.