1

I have a list of film titles and time from my class attributes. I have displayed this list with:

for i in range(0, len(films)):
     print(i, films[i].title, films[i].time)

This gives me a list of the titles in number and time.

Now I want to get at any item in title so that I could make calculation on based on the choice with number of seats.

I tried this:

i = int(input("Please select from our listings :"))
while i <= films[i].title:
    i = input("Please select from our listings :")
    if i in films[i].title:
        print("You have selected film: ",films[i].title)
        print("Regular seat: ", choice[regular], "\nVip Seat: ", choice[vip], "\nDiamond Seat: ", choice[diamond], "\nPlatinum Seat: ", choice[platinum], "\nDisability Regular Seat: ", disabilityRegular, "\nDisability Vip Seat: ", disabilityVip, "\nDisability Diamond Seat", disabilityDiamond, "\nDisability Platinum Seat", disabilityPlatinum )
        seatType = input("\nSelect your seat from these list: ")
        seating = int(input("How many seats: "))

        if seating == items in choice:
            total = seating*altogether[seatType]
            print(total) 

when run it displays this:(Note the list starts from 0):

29 End of Watch 20:00
30 Gremlins 19:30
31 The Twilight Saga: Breaking Dawn part 2 20:00

Please select from our listings :6
Please select from our listings :4

Traceback (most recent call last):
  File "C:/Python32/cin.py", line 91, in <module>
    if i in films[i].title:
TypeError: 'in <string>' requires string as left operand, not int
3
  • 2
    "if i in films[i].title" is the problem. I'm not sure what you're actually trying to achieve, but it's trying to use an integer (i) in a comparison with a string (films[i].title). Commented Dec 7, 2012 at 10:01
  • 2
    also, check the difference between the first and third line.(int) Commented Dec 7, 2012 at 10:05
  • @Talvalin, what I am trying to achieve is if the user selects say a film in position 9, I want to access that position independently and not as a list in other to make a calculation. Commented Dec 7, 2012 at 10:13

2 Answers 2

1
if i in films[i].title:

tries to match an integer within a string. You have to convert the integer into a string first:

if str(i) in films[i].title:

But this will match 2 to names like '... part 2', but also '1492: Conquest of Paradise'.

If you want to find the number of the movie, try this:

for i, film in enumerate(films):
     print('{0:3} {1:30} {2:5}'.format(i, film.title, film.time))

while True:
    try:
        film = films[int(input("Please select from our listings :"))]
    except (ValueError, IndexError), e:
        # input is not an integer between 0 and len(films)
        continue

    # now we have a valid film from the list
    print("You have selected film: ",film.title)
    # ...
Sign up to request clarification or add additional context in comments.

4 Comments

how do I define valueError
ValueError is a built-in exception type, so you shouldn't need to define it. Can you provide a link to your full code via Pastebin or something similar? Also, can you confirm which version of Python is being used please. :)
I am using Python 3.2.3 here is the link to my Pastebin pastebin.com/b8P1JfU4 user name: Kezborn
I figured the problem with valueError is case sensitivity, its fixed now. Thanks guys!
0

If you're using the film id as a key, it might be worth using a dictionary to store your films instead of a list. That way you can just use "in" to check if your film id keys exists in the dictionary without having to worry about out of bounds exceptions.

class Film(object):
    def __init__(self, title, time):
        self.title = title
        self.time = time

films = {}
films[29] = Film("End of Days", "20:00")
films[30] = Film("Gremlins", "19:30")
films[31] = Film("The Twilight Saga: Breaking Dawn part 2", "20:30")

for k,v in films.iteritems():
    print (k, v.title, v.time)

while True:
    i = int(input("Please select from our listings:"))
    if i in films:
        print ("You have selected film: ", films[i].title)
        # select seat here
    else:
        continue

2 Comments

for k,v in films.iteritems(): probably won't return the list in that order…
Agh. Good point. Would an OrderedDict work better in this instance?

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.