【问题标题】:How do I concatenate strings in a while loop?如何在while循环中连接字符串?
【发布时间】:2015-01-29 06:01:49
【问题描述】:

所以我试图做到这一点,以便我可以键入多个字符串,它将连接所有字符串。但是每次它只返回一个字符串而不添加它们。

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)

【问题讨论】:

  • (a) 你的缩进搞砸了,(b) return s 立即终止函数。
  • 在你准备好之前不要返回
  • 而有问题的代码根本无法工作

标签: python string concatenation


【解决方案1】:

这就是我假设你正在尝试做的事情:

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

但实际上你应该这样做:

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

当你学习了 itertools 魔法后,你可以做出一些东西(诚然丑陋),比如......

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

【讨论】:

  • 使用iter 会比itertools 更干净:''.join(iter(lambda: input('I will echo your input until you enter return only: '), ''))
  • @iCodez 哦,那好多了。我不知道iter 接受了哨兵论证
【解决方案2】:

您的代码的问题是,您没有设置正确的break 条件,而是在读取第一个输入项后才返回。

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()

【讨论】:

    【解决方案3】:

    我已经在 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) 
    

    希望能成功!!

    【讨论】:

      【解决方案4】:

      在您的代码中,您总是返回 s,即用户输入的字符串。并且该返回将导致该函数说:'嘿,我完成了。你现在可以继续了。 return 语句之后的所有语句都不会被调用,你的程序会直接跳出循环。

      因此,删除循环中的所有返回,因为您不想在用户仍在输入字符串时结束函数。你应该考虑使用 raw_input() 因为普通的 input() 将只允许输入整数,像这样:

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

      您应该注意到,语句 a += s 与 a = a + s 相同。

      接下来,当用户输入字符串时,循环中的输入消息可能会分散用户的注意力。您可以向他打印一条消息,然后在循环中请求输入而没有消息。但是,显然,您的代码并不需要这样。举个例子:

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

      最后,要优化的一件事是结束循环的条件。就像现在一样,它总是会再次添加字符串。 为了解决这个问题,你可以在你的 while 循环中添加一个条件来检查用户是否输入了一个空字符串:

      if s == '':
          break
      

      然后你可以把循环改成:

      while True:
           # ...
      

      现在你只需要在while循环之后返回整个字符串。

      while True:
          # ...
      return a
      

      一段代码中的所有这些更改将如下所示:

      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
      

      我在手机上回答这个问题,如有错误请见谅。

      希望能帮到你。 1Darco1

      【讨论】:

        猜你喜欢
        • 2017-01-20
        • 2017-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-15
        相关资源
        最近更新 更多