简体   繁体   English

如何将 for 循环中的多个字符串输入添加到 Python 中的单个变量?

[英]How can I add multiple string inputs in a for loop to a single variable in Python?

I'm a new coder and started coding in Python at school, but my school program is so slow (and now even slower because of covid-19) that i've kind of fallen down a well... how again do you add a string input to an empty string variable?我是一名新编码员,在学校开始使用 Python 编码,但我的学校课程太慢了(现在因为 covid-19 更慢了),我有点跌倒了……你又如何添加一个空字符串变量的字符串输入?

Let's say I enter 'frog', 'cow', and 'duck'.假设我输入了“frog”、“cow”和“duck”。 This code only returns "duck".此代码仅返回“鸭子”。 What do I change for it to return "frog cow duck"?我要改变什么让它返回“青蛙牛鸭”? Thanks:)谢谢:)

Here is what i've tried:这是我尝试过的:

for i in range(5):
    word_list = ""
    word = input("Enter a word for the word list: ")
    word_list = word_list + word + ""   

print word_list

you are reinitializing word_list inside the for loop, this is a logic error.您正在 for 循环中重新初始化 word_list,这是一个逻辑错误。

x1,x2,x3 = input("Enter Name: ").split() #frog cow duck

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

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