简体   繁体   中英

How do I concatenate strings in a while loop?

So I'm trying to make it so I can type multiples strings and it will concatenate all of them. But everytime it just returns one string and doesn't add them.

def addWords():
    s = 'a'
    while s != '':
        s = input( ' I will echo your input until you enter return only: ')
        return(s)
        a = a + s
        return (a)

Here's what I assume you're trying to do:

def add_words():
    a = ''
    s = 'a'
    while s != '':
        s = input("I will echo your input until you enter return only: ")
        a += s # equivalent to a = a + s
    # we exit the code block when they enter the empty string
    return a

But really you should do it like this:

def add_words():
    accumulator = ''
    while True:  # loop forever
        s = input("I will echo your input until you enter return only: ")
        if not s:  # if s is the empty string...
            break  # leave the infinite loop
        accumulator += s
    return accumulator

And when you learn itertools magic you could make something (admittedly ugly) like...

def add_words():
    return "".join(iter(lambda: input("I will echo your input until you enter return only: "), ''))

Issue with your code is, you did not put proper break condition, rather your just returned after reading first input item.

def addWords():
    resultant = ''
    delimiter = ' '
    while True:
        user_input = raw_input('I will echo your input until you enter return only:') # use raw_input() for python2
        if not user_input:
            break
        resultant += delimiter + user_input
    return resultant
addWords()

I have implemented it in python 2.7

def addwords():
      s = 'a'
      a = ''
      while s != '':
              s = raw_input( ' I will echo your input until you enter return only: ') # python 2.7 syntax
              a = a + s
      return (a) 

Hope it will work !!

In your code, you are always returning s, the string the user entered. And that return will cause the function so say: 'Hey, I'm done. You can go on now.' All the statements after the return statements won't get called and your program will jump out of the loop directly.

Therefore, remove all of the returns im your loop, as you don't want to end the function while the user is still entering their strings. And you should consider using raw_input() because the normal input() will obly allow entering integers, like this:

while ...:
    s = raw_input("...")
    a += s

You should notice, that the statement a += s is the same as a = a + s.

Next, the input message in your loop will probably distract the user a lot, when he is entering his strings. You could print a message to him and request input without a message in the loop then. However, thats not neccessary for your code to work, obviously. Here an example:

print "Hey, you can enter strings as long as you dont hit enter directly."
while ...:
     s = raw_input()
     # go on

Finally, one thing to optimize would be your condition to end the loop. As it is now, it will always add the string once more. To solve this you can add a condition in your while-loop to check if the user entered an empty string:

if s == '':
    break

Then you can change the loop to:

while True:
     # ...

And now you just need to return the whole string after the while loop.

while True:
    # ...
return a

All these changes in one piece of code will look like this:

def addWords():
    print "Hey, you can enter strings as long as you dont hit enter directly."
    a = ''
    while True:
        s = raw_input()
        if s == '':
            break
        a += s
    return a

I am answering this on my mobile, so please excuse any mistakes.

I hope I could help you. 1Darco1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM