首页 > 解决方案 > 变量在子程序中没有获得价值

问题描述

总成本不会打印或获得价值。我试过单独运行子程序,但没有奏效。它根本不会打印总成本:

            #coffee maker program

            print("Welcome to the BartSucks Coffee App")
            print("We will guide you through the ordering process")
            print("And our amazing Barista 'Simpson' will then serve you")

            name = input("Please type in your name: ")

            print("Would you like small, medium or large?")
            size = input("Type s for small\nType m for medium\nType l for large\n")
            while size.upper() not in ("S","M","L"):
                print("You must enter s, m or l")
                size = input("Please try again\n")


            print("Would you like zero,one, two or three spoons of sugar?")  
            sugars = input("Type 0 for none\nType 1 for one\nType 2 for two\nType 3 for three\n")
            while sugars not in ("0","1","2","3"):
                print("You must enter 0, 1, 2 or 3")
                sugars = input("Please try again\n")

            print("Would you like no syrup flavouring?")
            print ("Or would you like almond, vanilla or butterscotch syrup?")  
            flavour = input("n = none\na = almond\nv = vanilla\nb = butterscotch\n")
            while flavour.upper() not in ("N","A","V","B"):
                print("You must enter n, a, v or b")
                flavour = input("Please try again\n")





 totalcost=0    
            def CoffeeSize(cs):
                cs=cs.upper()
                global totalcost
                if size =="S" or size=="s":
                 totalcost+= 2.5
                elif size=="M" or size=="m":
                 totalcost+=3.0
                elif size=="L" or size=="l":
                 totalcost+= 3.5

            def SugarAmount(sa):
                sa=sa.upper()
                global totalcost
                if sugars=="0":
                 totalcost+= 0
                elif sugars=="1":
                 totalcost+= 0.5
                elif sugars=="2":
                 totalcost+= 1.0
                elif sugars=="3":
                 totalcost+= 1.5

            def flavour(fl):
                fl=fl.upper()
                global totalcost
                if flavour=="NONE" or flavour=="none":
                 totalcost+= 0
                elif flavour=="BS" or flavour=="bs":
                 totalcost+= 1.6
                elif flavour=="V" or flavour=="v":
                 totalcost+= 0.75
                elif flavour=="A" or flavour=="a":
                 totalcost+= 1.0

                CoffeeSize(cs)
                SugarAmount(sa)
                flavour(fl)
                print(totalcost)

标签: python

解决方案


对不起,我对此很陌生,所以如果我错了,请纠正我,但我认为问题是你在一个没有被执行的函数中调用函数?此外,除了 'if'、'def'... 等语句之外的所有内容都应该在第一个缩进级别

你的代码:

totalcost=0
def flavour(fl):
    ...
    ...
    CoffeeSize(cs)
    SugarAmount(sa)
    flavour(fl)
    print(totalcost)

在 Python 中,缩进很重要,它定义了它在哪个语句下运行。如您所见,您在与函数'flavour'下的代码相同的缩进级别调用函数,因此它不会被执行,因为没有任何其他地方调用该函数。尝试将其放在程序的末尾:

代码:

if __name__ == '__main__':
    CoffeeSize(cs)
    SugarAmount(sa)
    flavour(fl)
    print(totalcost)

它的作用是检查程序是否是主程序,而不是被其他程序导入。如果是 main/'_ main_ ' 程序,它会从头开始,询问用户他们想要什么,然后检查这个程序是否是 main 程序,然后执行 if 下列出的所有函数陈述。

抱歉,如果我误解了您的问题,但我认为从我的角度来看,这就是问题所在:) 谢谢!


推荐阅读