因此,当我输入除儿童,成人和老人或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

09-20 12:55