总成本不会打印或获得价值。我尝试过单独运行子例程,但这没有用。它根本不会打印出总费用:
#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)
答案 0 :(得分:2)
对不起,我对此很陌生,因此如果我错了,请纠正我,但我认为问题是您正在调用未执行的函数中的函数? 此外,除了“ if”,“ def” ... etc语句下的内容外,其他所有内容都应位于第一个缩进级别
您的代码:
totalcost=0
def flavour(fl):
...
...
CoffeeSize(cs)
SugarAmount(sa)
flavour(fl)
print(totalcost)
在Python中,缩进非常重要,并且缩进在它运行的语句下进行定义。 如您所见,您正在以与'flavor'函数下的代码相同的缩进级别调用这些函数,因此该函数将不会执行,因为没有其他地方可以调用该函数。尝试将其放在程序的末尾:
代码:
if __name__ == '__main__':
CoffeeSize(cs)
SugarAmount(sa)
flavour(fl)
print(totalcost)
这是要检查程序是否为主程序,而不是由其他程序导入。如果它是main /'_ main _'程序,它将从头开始,询问用户他们想要什么,然后检查该程序是否为主要程序,然后执行所有if语句下列出的函数。
对不起,如果我误解了您的问题,但我认为这就是我的问题所在:) 谢谢!