电影预订程序:TypeError:不支持的操作数类型 - :'list'和'int'

时间:2014-11-20 20:35:41

标签: python list validation int typeerror

所以这是我为我的计算gcse课程编写的一个程序,我只学习python几个月,所以请原谅我的无能。当你运行它时,你会得到标题中的错误。我理解这与将整数转换为列表有关,但我需要快速帮助,因为这篇文章将在今天发布。提前谢谢。

Seats = [] #Each Row has ten seats available atm

print("""
E1 E2 E3 E4 E5 E6 E7 E8 E9 E10
D1 D2 D3 D4 D5 D6 D7 D8 D9 D10
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10
B1 B2 B3 B4 B5 B6 B7 B8 B9 B10
A1 A2 A3 A4 A5 A6 A7 A8 A9 A10""")
# This will help people to understand where they are booking
Data = open('Cinema.csv', 'r+') #Open and reads and allows to be written in  file
line = Data.readlines()
if line!='':
    Seats = [line]
#The proccess above checks whether the .csv file contains anything and if
#it does then it sets the list seats to the values in the file.
else:
    Seats = [10,10,10,10,10]
#sets seats to all empty when there is nothing in there is nothing on thr .csv file
EntryPeople = 0
while True: #loops back to here and askes question angain on line bellow
    try:
        EntryPeople = int(input("How many people do you wish to book for? MAXIMUM 6!"))
    except ValueError:
        print("You cant type that in its got to be an Integer")
    else:
#This section makes sure that what is entered is an integer and pastes that they need
#to enter one if they wish to continue.
        while EntryPeople >6: #Loops back to this point if they enter more than 6
            print("Please Enter a Number bellow six!") #Reminds them
            EntryPeople = int(input("Many people do you wish to book for? MAXIMUM 6!"))
#This part above puts a cap on how man people they can book at once
        else:
            EntryRow = input("Which row would you like to book for?")
            Upper = EntryRow.upper()
            while not Upper in ('A','B','C','D','E'):
                print("That is not a row")
                EntryRow = input("Which row would you like to book for?")
                Upper = EntryRow.upper()
#Checks whether the row is valid
            else:
                Index = ord(EntryRow.upper()) -65
                if Seats[Index]- EntryPeople > -1:
                    Seats[Index] = Seats[Index] - EntryPeople
                    print("Thank you for booking seats on row: " +EntryRow.upper())
                    Data = open('Cinema.csv', 'wt')
                    for item in Seats:
                        Data.write("%s\n" % item)#copies whats in the list to the file
                    Data.close()
#This segmant above forces the input to uppercase because all uppercase have a value. A = 65 if       you
#subtract 65 form 65 you get 0. 0 is the corresponding number to A in the list.
#Therefore B=1; it works for any value entered.
                else:
                    print("i'm Sorry but those seats are currently unavailable")
                    print("Available seats on row A:")
                    print(Seats[0])
                    print("Available seats on row B:")
                    print(Seats[1])
                    print("Available seats on row c:")
                    print(Seats[2])
                    print("Available seats on row D:")
                    print(Seats[3])
                    print("Available seats on row E:")
                    print(Seats[4])#pretty prints free seats
                    Data.close()
                    import sys
                    sys.exit()
 #Copies Booked seats into a file

1 个答案:

答案 0 :(得分:1)

至于我现在发现的是第11行

line = Data.readlines()

总是返回一个列表,因此下一个if语句(第12行)已被执行,因为列表始终是!=' '

if line!='':
    Seats = [line]

结果是Seats是第42行中列表和语句的列表

Seats[Index]

返回一个列表,你不能从列表中减去一个int。因此TypeError

我想以下应该做的工作

if line:
    Seats = [int(i) for i in line]