python - 未绑定(bind)本地错误: local variable 'ticketCost' referenced before assignment

标签 python

因此,当我输入 child 、成人和老年人或 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 方法的最后一行就是发生这种情况的地方,原因是仅当您的 ticketType 时才会创建 ticketCost > 匹配您列出的类别之一。您可以考虑在末尾创建一个 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

关于python - 未绑定(bind)本地错误: local variable 'ticketCost' referenced before assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52428830/

相关文章:

python - 如何在 Python 中打印不带空格的变量字符串(最少的编码!)

python - 在维度内建立索引的最简单方法

python - 将 Matplotlib 图转换为没有任何边框/框架或轴的 NumPy 数组

python - Python 中的可链接动态属性

python - Django channel 不会使用 CHANNEL_LAYERS 启动 redis 服务器

Python 排序不起作用

python - Scapy DNS 请求

python - 键盘输入超时?

python - 几个小时后 Django + PyMySQL 抛出 "MySQL server has gone away"

python - 在 Mac OS 10.9 上使用 python3 interpeter 制作 virtualenv 时出错