Поэтому я хочу, чтобы появилось сообщение об ошибке "Извините, недействительный ввод", когда я вводил ничего, кроме ребенка, взрослого и старшего, или 3D и 2D. И затем я хочу, чтобы он снова попросил пользователя ввести тип ticketType и movieType, но он
"File "python", line 33, in <module>
File "python", line 27, in buyOneTicket
UnboundLocalError: local variable 'ticketCost' referenced before assignment"
,
print ('Welcome to RareCinema Ticketing System')
num = int(input('How many tickets would you like to buy?'))
def buyOneTicket() :
ticketType = input('Enter the type of ticket (Child/Adult/Senior)?')
movieType = input('Enter the movie type (2D/3D)?')
if ticketType == ("child"):
if movieType == ("2D"):
ticketCost = 16
elif movieType == ('3D'):
ticketCost = 19
elif ticketType == ('adult'):
if movieType == ('2D'):
ticketCost = 22
elif movieType == ('3D'):
ticketCost = 27
elif ticketType == ('senior'):
if movieType == ('2D'):
ticketCost = 14
elif movieType == ('3D'):
ticketCost = 18
return ticketCost
ticketCost=0
count = 1
while (count <= num):
ticketCost = ticketCost + buyOneTicket()
count = count + 1
if ticket_type != ('child') and ('adult') and ('senior'):
if movie_type != ('2D') and ('3D'):
print ('Sorry, you have entered an invalid input')
ticketType = input('Enter the type of ticket (Child/Adult/Senior)?')
movieType = input('Enter the movie type (2D/3D)?')
else:
print('Your total cost for ' +str(num)+ ' is ', ticketCost )
def buyOneTicket() :
ticketType = input('Enter the type of ticket (Child/Adult/Senior)?')
movieType = input('Enter the movie type (2D/3D)?')
if ticketType == ("child"):
if movieType == ("2D"):
ticketCost = 16
elif movieType == ('3D'):
ticketCost = 19
elif ticketType == ('adult'):
if movieType == ('2D'):
ticketCost = 22
elif movieType == ('3D'):
ticketCost = 27
elif ticketType == ('senior'):
if movieType == ('2D'):
ticketCost = 14
elif movieType == ('3D'):
ticketCost = 18
return ticketCost
Таким образом, последняя строка вашего метода buyOneTicket
- это то, где это происходит, и причина в том, что ticketCost
только тогда, когда ваш ticketType
соответствует одной из ваших перечисленных категорий. Вы можете рассмотреть возможность создания в конце условия else
которое снова спросит пользователя:
def buyOneTicket() :
ticketType = input('Enter the type of ticket (Child/Adult/Senior)?')
movieType = input('Enter the movie type (2D/3D)?')
if ticketType == ("child"):
if movieType == ("2D"):
ticketCost = 16
elif movieType == ('3D'):
ticketCost = 19
elif ticketType == ('adult'):
if movieType == ('2D'):
ticketCost = 22
elif movieType == ('3D'):
ticketCost = 27
elif ticketType == ('senior'):
if movieType == ('2D'):
ticketCost = 14
elif movieType == ('3D'):
ticketCost = 18
else:
print('You did not pick a valid ticket type. Try again.')
return buyOneTicket()
return ticketCost