0

OK, So far I only know C# and I have started learning 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. Trying to do the same with 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

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. Trying to do the same in 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". 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. But I want it to print out "Enter the name of film number 'i' " to make more sense. Can someone help me with this aswell if you get what am trying to do?

2 Answers 2

1

In Python, you would generally do this as follows:

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. You can't explicitly set the upper limit of a list's indices, but this will only append NumOfFilms to FilmList.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much, but a further question. So what exactly am I doing with FilmList = [NumOfFilms]? Just creating a list with the value of the variable NumOfFilms?
You create a single-element list (len(FilmList) == 1) that contains a single integer (FilmList[0] == NumOfFilms).
I figured out {0} is being used to set the value of the variable "n" to 0, but its the first time I seen something like this. Do you mind explaining what you exactly did here? How did you know about .format, and is there a list of stuff like that? I really cant find any decent tutorials on the syntax of python
No, {0} tells format to replace that placeholder with the first (0th) argument. There is lots of good material on the Python site; follow the link in my answer.
Ah sorry, didnt even see that was a link. This looks promising, thank you very much sir
0

The statement FilmList = [NumOfFilms] does not create a list of size NumOfFilms. It will just create a list with one element which is 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]=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.

Regarding the second question, function input() takes only one string argument. If you also want to print the number i then you simply can concatenate 'i' to the string you are displaying as:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.