[英]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.