首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UnboundLocalError:赋值前引用的局部变量“ticketCost”

UnboundLocalError:赋值前引用的局部变量“ticketCost”
EN

Stack Overflow用户
提问于 2018-09-20 15:45:59
回答 1查看 145关注 0票数 1

因此,当我输入儿童、成人和老年人或3D和2D以外的任何内容时,我希望出现一条错误消息,说“对不起,无效输入”。然后我想让它再次要求用户输入ticketType和movieType,但是它想到了

代码语言:javascript
复制
"File "python", line 33, in <module>
  File "python", line 27, in buyOneTicket
UnboundLocalError: local variable 'ticketCost' referenced before assignment"

代码语言:javascript
复制
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 )
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-20 15:51:15

代码语言:javascript
复制
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条件,这将再次询问用户:

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52428830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档