简体   繁体   English

Python-需要帮助,使用python将用户输入存储在列表中

[英]Python - Need Help Getting User input stored in a list using python

I have this assignment where i have to create a list asking users to input 3 or 5 of their favorite movies, then im suppose to take that input and create a list with it and after display the list. 我要执行此作业,我必须创建一个列表,要求用户输入3或5个他们最喜欢的电影,然后假定使用该输入并用它创建一个列表,然后显示该列表。

limit = 3

movieslist = []

while len(movieslist) < limit:  

    movie = raw_input("Enter The Name Of Your favorite Netflix movie" )
    print 
    movieslist.append(movie)

print "The Following Is A List Of Your Top 3 Favorite Netflix Movies:"

for x in movieslist:
    print x

Your professor's request is weird and I don't know if it'll satisfy her, but this'll work: 您的教授的要求很奇怪,我不知道是否可以满足她的要求,但这是可行的:

movieslist.extend([movie])

or equivalently: 或等效地:

movieslist += [movie]

This would also work but isn't using a while loop: 这也可以,但是不使用while循环:

movieslist = [raw_input("....") for i in range(limit))]

You can use the insert method 您可以使用插入方法

print "In the Following Program Enter Your Top 10 Favorite Netflix Movies When Prompted"

print ""

limit = 10

movieslist = []

while len(movieslist) < limit:  

    movie = raw_input("Enter The Name Of Your Top Movie(s) From Netflix" )
    print 
    movieslist.insert(0,movie)

print "The Following Is A List Of Your Top 10 Favorite Netflix Movies:"

for x in movieslist:
    print x

Here's a roundabout way if you want to troll/impress/confuse your professor: 如果您想拖曳/打动/迷惑您的教授,这是一种回旋方式:

def movie_generator():
    i = 0
    limit = 3
    while i < limit:
        i += 1
        yield raw_input("prompt")

movieslist = list(movie_generator())

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

相关问题 需要帮助从用户那里获取输入作为列表 - Need help getting an input from the user as a list 如果用户输入错误,我不断收到语法错误 - 需要帮助 Python 创建矢量计算器 - I keep getting a syntax error in else - need help Python creating a vector calculator if the user put the wrong input 在python列表中需要帮助 - need help in python list 需要帮助将用户输入写入 python 中 CSV 文件的新行 - Need help writing user input into new row on CSV file in python 需要使用python 2.7.1的帮助,以根据用户输入选择并运行各种批处理文件 - Need help using python 2.7.1 to select and run various batch files based on user input 在python 3中从用户获取数字输入列表 - Getting numerical input list from user in python 3 需要帮助让 while 循环与 python 中的列表一起使用 - Need help getting a while loop to work with a list in python 需要有关Python列表操作的帮助 - Need help with python list manipulation 我需要将用户输入变量作为此列表传递,例如:python 程序下方的 [1, 3, 6, 4, 1, 2]。 有人可以帮我吗 - I need to pass a user input variable as this list eg: [1, 3, 6, 4, 1, 2] in below python program. Could some one help me 将用户输入值与python列表中字典中存储的值进行比较 - Compare user input values with the values stored in dictionaries in list in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM