I want to create a list from a user's input and then return a random value from that list.
我想从用户的输入创建一个列表,然后从该列表中返回一个随机值。
This is what I have so far for the code but I can't figure out how to get input for the array.
这是我到目前为止的代码,但我无法弄清楚如何获取数组的输入。
import random
movie_array = ["movieA", "movieB", "movieC"]
print(random.choice(movie_array))
I know about the input function but I'm not sure how to use it to create an array/list. I tried the following but it didn't work
我知道输入函数,但我不知道如何使用它来创建数组/列表。我试过以下但是没有用
movie_array = input()
but when I run the random.choice on that it only selects an individual character.
但是当我运行random.choice时,它只选择一个单独的字符。
5 个解决方案
#1
2
You can do it like this:
你可以这样做:
>>> import random
>>> movie_array = [input("Input a movie: ") for i in range(3)]
Input a movie: movieA
Input a movie: movieB
Input a movie: movieC
>>> print(random.choice(movie_array))
movieC
#2
1
Use a loop.
使用循环。
In the body of the loop prompt the user to enter a movie title, or 'q' to quit. Append each entry to your movie list. When finished select a random movie from the movie list:
在循环体中提示用户输入电影标题,或者“q”退出。将每个条目附加到电影列表中。完成后从影片列表中选择一个随机影片:
import random
movies = []
while True:
entry = input('Enter a movie title (q to quit): ')
if entry.lower() == 'q':
break
movies.append(entry)
if movies:
print(random.choice(movies))
Hopefully your users do not want to enter the movie entitled "Q" (1982). It might be better to use an empty entry as the sentinel, like this:
希望您的用户不想进入名为“Q”(1982)的电影。使用空条目作为标记可能更好,如下所示:
entry = input('Enter a movie title (<Enter> to quit): ')
if entry == '':
break
#3
0
You can read movies in a single line separated by commas and then split it into array.
您可以用逗号分隔的单行读取电影,然后将其拆分为数组。
movie_array=raw_input("Enter Movies").split(',')
If it is python 3.x
如果是python 3.x.
movie_array=input("Enter Movies").split(',')
#4
0
Just want to clarify why your solution attempt didnt work IE this:
只是想澄清为什么你的解决方案尝试没有工作IE浏览器:
movie_array = input()
It's because the input returns a string
I.E a list of substrings so you can seemovie_array = 'test'
asmovie_array = ['t','e','s','t']
这是因为输入返回一个字符串I.E一个子字符串列表,所以你可以看起来像是一个'测试'asmovie_array = ['t','e','s','t']
print(movie_array[1])
>>>'e'
in order to solve your problem I would recommend @mhawke answer (using a while
loop)
为了解决你的问题我会建议@mhawke回答(使用while循环)
By using a while
loop you can retrieve multiple inputs and store each of them in a list. Be aware though that a while loop has to be able to "END" or it can cause problems. Therefore you have to have a break
somewhere in the while
loop. Otherwise the program would be run indefinetly until you either forced it to shutdown or until it crashes.
通过使用while循环,您可以检索多个输入并将每个输入存储在列表中。请注意,虽然while循环必须能够“结束”,否则可能会导致问题。因此,您必须在while循环中的某处休息。否则,程序将被无限运行,直到您强制它关闭或直到它崩溃。
Further information about this can be read here:
有关这方面的更多信息,请访问:
also checkout the official wiki for strings and control flow.
还检查官方维基的字符串和控制流程。
#5
-1
You can give it like this.
你可以像这样给它。
movie_array = [b for b in input().split()]
And then you can get input from user like this,
然后你就可以得到像这样的用户输入,
movieA movieB movieC
#1
2
You can do it like this:
你可以这样做:
>>> import random
>>> movie_array = [input("Input a movie: ") for i in range(3)]
Input a movie: movieA
Input a movie: movieB
Input a movie: movieC
>>> print(random.choice(movie_array))
movieC
#2
1
Use a loop.
使用循环。
In the body of the loop prompt the user to enter a movie title, or 'q' to quit. Append each entry to your movie list. When finished select a random movie from the movie list:
在循环体中提示用户输入电影标题,或者“q”退出。将每个条目附加到电影列表中。完成后从影片列表中选择一个随机影片:
import random
movies = []
while True:
entry = input('Enter a movie title (q to quit): ')
if entry.lower() == 'q':
break
movies.append(entry)
if movies:
print(random.choice(movies))
Hopefully your users do not want to enter the movie entitled "Q" (1982). It might be better to use an empty entry as the sentinel, like this:
希望您的用户不想进入名为“Q”(1982)的电影。使用空条目作为标记可能更好,如下所示:
entry = input('Enter a movie title (<Enter> to quit): ')
if entry == '':
break
#3
0
You can read movies in a single line separated by commas and then split it into array.
您可以用逗号分隔的单行读取电影,然后将其拆分为数组。
movie_array=raw_input("Enter Movies").split(',')
If it is python 3.x
如果是python 3.x.
movie_array=input("Enter Movies").split(',')
#4
0
Just want to clarify why your solution attempt didnt work IE this:
只是想澄清为什么你的解决方案尝试没有工作IE浏览器:
movie_array = input()
It's because the input returns a string
I.E a list of substrings so you can seemovie_array = 'test'
asmovie_array = ['t','e','s','t']
这是因为输入返回一个字符串I.E一个子字符串列表,所以你可以看起来像是一个'测试'asmovie_array = ['t','e','s','t']
print(movie_array[1])
>>>'e'
in order to solve your problem I would recommend @mhawke answer (using a while
loop)
为了解决你的问题我会建议@mhawke回答(使用while循环)
By using a while
loop you can retrieve multiple inputs and store each of them in a list. Be aware though that a while loop has to be able to "END" or it can cause problems. Therefore you have to have a break
somewhere in the while
loop. Otherwise the program would be run indefinetly until you either forced it to shutdown or until it crashes.
通过使用while循环,您可以检索多个输入并将每个输入存储在列表中。请注意,虽然while循环必须能够“结束”,否则可能会导致问题。因此,您必须在while循环中的某处休息。否则,程序将被无限运行,直到您强制它关闭或直到它崩溃。
Further information about this can be read here:
有关这方面的更多信息,请访问:
also checkout the official wiki for strings and control flow.
还检查官方维基的字符串和控制流程。
#5
-1
You can give it like this.
你可以像这样给它。
movie_array = [b for b in input().split()]
And then you can get input from user like this,
然后你就可以得到像这样的用户输入,
movieA movieB movieC