简体   繁体   English

将多个输入存储在一个变量中

[英]Storing multiple inputs in one variable

I have a while loop, which will keep asking a user to input words until they type stop. 我有一个while循环,它将不断要求用户输入单词,直到他们键入stop。 The input is stored in a variable called sentence. 输入存储在称为句子的变量中。 My question is how do I store multiple inputs into one variable. 我的问题是如何将多个输入存储到一个变量中。

My current code is 我当前的代码是

stop = "stop"   
sentence = []
while sentence != stop:
    sentence = input("Enter a word: ")
    sentence = sentence
    print(sentence)

I don't understand how I would keep storing variables from one input and print out all the variable stored separated by commas/spaces etc 我不明白如何继续从一个输入中存储变量并打印出所有存储的变量(用逗号/空格等分隔)

stop = "stop"
# okay --- 'sentence' is a list. Good start.   
sentence = []
while sentence != stop:
    # ...but now you've replaced the list 'sentence' with the word that was just input
    # NOTE that in Python versions < 3, you should use raw_input below.
    sentence = input("Enter a word: ")
    # ...and this does nothing.
    sentence = sentence
    print(sentence)

Works better if you do something like this: 如果您执行以下操作,效果会更好:

stop = "stop"   
sentence = []
# create a new variable that just contains the most recent word.
word = ''
while word != stop:
    word = input("Enter a word: ")
    # stick the new word onto the end of the list
    sentence.append(word)
    print(sentence)
# ...and convert the list of words into a single string, each word
# separated by a space.
print " ".join(sentence)

...or to re-design a bit to omit the 'stop', something like: ...或重新设计以省略“停止”,例如:

stop = "stop"   
sentence = []
while True:
    word = input("Enter a word: ")
    if word == stop:
        # exit the loop
        break
    sentence.append(word)

# ...and convert the list of words into a single string, each word
# separated by a space.
print " ".join(sentence)

All you need to do is append() your new variables to the array: 您需要做的就是append()新变量append()到数组中:

>>> a = []
>>> for x in range(5):
...     a.append("Hello!")
... 
>>> a
['Hello!', 'Hello!', 'Hello!', 'Hello!', 'Hello!']

At the end, if you need everything in a single variable you can use join() : 最后,如果您需要将所有内容都放在一个变量中,则可以使用join()

>>> ",".join(a)
'Hello!,Hello!,Hello!,Hello!,Hello!'

Its pretty simple 非常简单

stop = "stop"   
sentence = []
all = ""
while sentence != stop:
    sentence = input("Enter a word: ")
    all += sentence + ","
    print(all)

You probably want something like this: 您可能想要这样的东西:

sentence = []

while True:
    word = input("Enter a word: ")
    if word == "stop":
        break
    sentence.append(word)

print " ".join(sentence) + "."

One of your problems is that you are constantly writing over your sentence variable. 您的问题之一是您不断地重写句子变量。

What you want to do is make use of the list append method. 您要做的是利用list append方法。 Documentation on lists: 清单上的文档:

https://docs.python.org/3/tutorial/datastructures.html https://docs.python.org/3/tutorial/datastructures.html

Example: 例:

a = []
a.append(1)
a.append(2)
a.append(3)
print(a)
[1, 2, 3]

Next, you are looking to end your code if the user enters "stop". 接下来,如果用户输入“停止”,则希望结束代码。 So what you should do is check in your loop if "stop" was written, and make use of Python's break , to break out of the loop. 因此,您应该做的是检查循环是否已编写“ stop”,并利用Python的break来中断循环。

What this means is that you should change your loop to loop indefinitely until you get that stop , using while True . 这意味着您应该使用while True来将循环更改为无限循环,直到您stop该循环为止。

Your code can now simply look like this: 您的代码现在可以像下面这样:

sentence = []
while True:
    entry = input("Enter a word: ")
    if entry == "stop":
        break
    sentence.append(entry)
    print(sentence)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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