简体   繁体   English

如何在python 3中设置列表/数组的最大元素

[英]How to set the maximum element of a list/array in python 3

OK, So far I only know C# and I have started learning python. 好的,到目前为止,我只知道C#,并且已经开始学习python。 To learn C# I made a cinema program which shows a list of films and lets the user pick and enter their age to see if they are old enough. 为了学习C#,我制作了一个电影程序,该程序显示电影列表,并让用户选择并输入年龄,以查看它们是否足够大。 Trying to do the same with python. 试图用python做同样的事情。

At the start, the person chooses how many films are being shown and then is allowed to enter film names for each film up to the maximum amount of films being shown. 开始时,人员选择要显示的电影数量,然后允许输入每部电影的电影名称,最多显示所显示的电影数量。 This is my attempt in python 这是我在python中的尝试

print("Welcome to our multiplex:")
NumOfFilms = input("How many films are being shown?")
NumOfFilms = int(NumOfFilms)
FilmList = [NumOfFilms]

i = 0
while i < NumOfFilms:
    FilmList[i].append = input("Enter the name of film number",i)
    i = i + 1

In C# I did a similar thing, set the maximum element of an array I created equal to the NumOfFilms variable. 在C#中,我做了类似的事情,将我创建的数组的最大元素设置为等于NumOfFilms变量。 Trying to do the same in python. 试图在python中做同样的事情。 The variable "i" is being used as an element placer for the list here.However whenever I run this, one of the errors I get is that "list assignment index is out of range". 变量“ i”在这里用作列表的元素放置。但是,每当我运行此变量时,我遇到的错误之一就是“列表分配索引超出范围”。 Can someone help me please? 有人能帮助我吗?

There is also another problem where the function input doesn't except 2 arguments in this case a string and an integer i. 还有一个问题,函数输入除了两个参数(在这种情况下为字符串和整数i)之外,没有其他问题。 But I want it to print out "Enter the name of film number 'i' " to make more sense. 但我希望它打印出“输入电影编号'i'的名称”以使其更有意义。 Can someone help me with this aswell if you get what am trying to do? 如果您知道要做什么,可以有人帮助我吗?

In Python, you would generally do this as follows: 在Python中,通常应按以下步骤进行操作:

for n in range(NumOfFilms):
   title = input("Enter the name of film number {0}".format(n))
   FilmList.append(title)

Note the use of str.format to generate a single string for input , and the fact that (as Python doesn't support initialising indices for the built-in list ) a bare append to add items to the end of it. 请注意,使用str.formatinput生成了一个字符串,并且(由于Python不支持内置list初始化索引)仅添加了一个append项即可添加内容。 You can't explicitly set the upper limit of a list's indices, but this will only append NumOfFilms to FilmList . 您不能显式设置列表索引的上限,但这只会append NumOfFilms appendFilmList

The statement FilmList = [NumOfFilms] does not create a list of size NumOfFilms . 语句FilmList = [NumOfFilms]不会创建大小为NumOfFilms的列表。 It will just create a list with one element which is NumOfFilms . 它将仅创建一个包含NumOfFilms元素的列表。

Instead of FilmList[i].append = input("Enter the name of film number",i) , FilmList.append(input("Enter the name of film number")) should work. 代替FilmList[i].append = input("Enter the name of film number",i)FilmList.append(input("Enter the name of film number"))

Filmlist[i]=input("Enter the name of film number") will insert the value from input to 'i' th position of list while append() , which just takes the name of list and not the index, will always insert the new value at end of the list. Filmlist[i]=input("Enter the name of film number")会将输入中的值插入到列表的第i个位置,而append()仅使用列表的名称而不是索引,它将始终插入列表末尾的新值。

Regarding the second question, function input() takes only one string argument. 关于第二个问题,函数input()仅采用一个字符串参数。 If you also want to print the number i then you simply can concatenate 'i' to the string you are displaying as: 如果您还想打印数字i,则只需将“ i”连接到显示为的字符串即可:

input("Enter the name of film number "+str(i))

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

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