Python 变量在子例程中没有获得值

Python 变量在子例程中没有获得值,python,Python,总成本不会打印或增值。我试过分别运行子程序,但没有成功。它根本不会打印totalcost: #coffee maker program print("Welcome to the BartSucks Coffee App") print("We will guide you through the ordering process") print("And our amazing Barista '

总成本不会打印或增值。我试过分别运行子程序,但没有成功。它根本不会打印totalcost:

            #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)

对不起,我对这一点很陌生,所以如果我错了,请纠正我,但我认为问题是,您正在调用一个未执行的函数中的函数? 此外,除了“if”和“def”下的任何内容之外的所有内容。。。etc语句应位于第一缩进级别

您的代码:

totalcost=0
def flavour(fl):
    ...
    ...
    CoffeeSize(cs)
    SugarAmount(sa)
    flavour(fl)
    print(totalcost)
在Python中,缩进很重要,它定义在哪个语句下运行。 正如您所看到的,您在与函数“flavor”下面的代码相同的缩进级别上调用函数,因此它不会被执行,因为没有任何其他地方调用该函数。请尝试将此内容放在程序的末尾:

代码:

这样做的目的是检查程序是否是主程序,而不是由其他程序导入。如果它是main/“main”程序,它将从头开始,询问用户想要什么,然后检查该程序是否是主程序,然后执行If语句下列出的所有函数

对不起,如果我误解了你的问题,但我认为这就是我的观点:)
谢谢

抱歉,我对这一点很陌生,如果我错了,请纠正我,但我认为问题在于您调用的函数是一个未执行的函数? 此外,除了“if”和“def”下的任何内容之外的所有内容。。。etc语句应位于第一缩进级别

您的代码:

totalcost=0
def flavour(fl):
    ...
    ...
    CoffeeSize(cs)
    SugarAmount(sa)
    flavour(fl)
    print(totalcost)
在Python中,缩进很重要,它定义在哪个语句下运行。 正如您所看到的,您在与函数“flavor”下面的代码相同的缩进级别上调用函数,因此它不会被执行,因为没有任何其他地方调用该函数。请尝试将此内容放在程序的末尾:

代码:

这样做的目的是检查程序是否是主程序,而不是由其他程序导入。如果它是main/“main”程序,它将从头开始,询问用户想要什么,然后检查该程序是否是主程序,然后执行If语句下列出的所有函数

对不起,如果我误解了你的问题,但我认为这就是我的观点:)
谢谢

包含“totalcost=0”的行的缩进方式与代码的其余部分不同。像这样,您没有有效的Python,至少在最后一个函数中,您将函数本身与值进行比较。(
if flavor==“NONE”
etc)另外,
cs
sa
等从何而来,为什么不使用传递给函数的参数,而使用外部范围的变量?为什么要先
upper
,然后检查这两种情况?首先修复代码缩进,然后学习避免全局缩进包含“totalcost=0”的行的缩进与代码的其余部分不同。像这样,您没有有效的Python,至少在最后一个函数中,您将函数本身与值进行比较。(
if flavor==“NONE”
etc)另外,
cs
sa
等从何而来,为什么不使用传递给函数的参数,而使用外部范围的变量?为什么要先检查上排,然后再检查这两种情况?首先修复代码缩进,然后学会避免全局缩进