Python中函数的全局范围

时间:2016-06-23 02:35:33

标签: python

在下面的代码中,我试图不使用" global"在我阅读的功能中,这是不好的做法。如果我删除它们代码将无法正常工作。我已经查看了很多关于将参数传递给其他函数的东西,但是当我尝试他们在示例中所拥有的内容时,我无法通过其他函数传递参数。

这是我的第一篇文章,所以如果我输入很多代码,我很抱歉。

def main():     
    while Order == "yes":
        print ("Enter 1 for Yum Yum Burger")
        print ("Enter 2 for Grease Yum Fries")
        print ("Enter 3 for Soda Yum")
        option = input('Enter number -> (or press "Enter" to get total) ')
        if option == "1":
            burgerChoice()
        elif option == "2":
            friesChoice()
        elif option == "3":
            sodaChoice()
        elif option == "123":
            burgerChoice()
            friesChoice()
            sodaChoice()
        elif option == "":
             menuTotal()
             break
        else:
            print ("You have entered an invalid option!!!")

def burgerChoice():
    global numBurgers
    global burgerTotal
    global extraBurgers
    numBurgers = int(input("How many Yum Yum Burgers would you like? "))
    questionBurgers = input("Is that all the Yum Yum Burgers you want?, y or n ")
    if questionBurgers == "y":
        extraBurgers = 0
        burgerTotal = numBurgers * 2.99
        return burgerTotal    
    else:
        extraBurgers = int(input("How many more would you like(or a negitive number to remove)? "))
        burgerTotal = (numBurgers + extraBurgers) * 2.99
        return burgerTotal
    print("You ordered {} Yum Yum Burgers" .format(numBurgers + extraBurgers))


def friesChoice():
    global numFries
    global friesTotal
    global extraFries
    numFries = int(input("How many Grease Yum Fries would you like? "))
    questionFries = input("Is that all the Grease Yum Fries you want?, y or n ")
    if questionFries == "y":
        extraFries = 0
        friesTotal = numFries * 1.99
        return friesTotal
    else:
        extraFries = int(input("How many more would you like(or a negitive number to remove)? "))
        friesTotal = (numFries + extraFries) * 1.99
        return friesTotal
    print("You ordered {} Grease Yum Fries" .format(numFries + extraFries))


def sodaChoice():        
    global numSodas
    global sodasTotal
    global extraSodas
    numSodas = int(input("How many Soda Yum would you like? "))
    questionSodas = input("Is that all the Soda Yum you want?, y or n ")
    if questionSodas == "y":
        extraSodas = 0
        sodasTotal = numSodas * .99
        return sodasTotal
    else:
        extraSodas = int(input("How many more would you like( a negitive number to remove)? "))
        sodasTotal = (numSodas + extraSodas) * .99
        return sodasTotal
    print("You ordered {} Soda Yum" .format(numSodas + extraSodas))

def menuTotal():
    print("\n")
    if numBurgers > 0:
        print("You have {:.0f} Yum Yum Burgers for a total of ${:.2f}" .format((numBurgers + extraBurgers),burgerTotal))
    if numFries > 0:
        print("You have {:.0f} Grease Yum Fries for a total of ${:.2f}" .format((numFries + extraFries),friesTotal))
    if numSodas > 0:
        print("You have {:.0f} Soda Yum for a total of ${:.2f}" .format((numSodas + extraSodas),sodasTotal))
    subTotal = (burgerTotal + friesTotal + sodasTotal)
    print("Your subtotal is ${:.2f}" .format(subTotal))
    tax = (burgerTotal + friesTotal + sodasTotal)*.0735
    print("Your tax is ${:.2f}" .format(tax))
    tip = (burgerTotal + friesTotal + sodasTotal)*.15
    print("Your recommended tip is ${:.2f}" .format(tip))
    total = (subTotal + tax)      
    print("Your total with tax (not including tip) is ${:.2f}"          .format(total))
    print("Your total with tax (including tip) is ${:.2f}" .format(total + tip))
#START OF PROGRAM         

numBurgers = 0
numSodas = 0
burgerTotal = 0
numFries = 0
friesTotal = 0
numSodas = 0
sodasTotal = 0
extraBurgers = 0
extraFries = 0
extraSodas = 0
print("Welcome to the Greasy Yum Diner! It is out pleasure to serve you!")
Order = input("Do you want to place your order? (Enter yes or no): ")
Order = Order.lower()
if Order == "yes":
    main()


elif Order != "no" and Order != "yes":
    Order = input('Please enter "yes" to start the menu. ')
    main() 

else:
    print("Thank you")


def burgerChoice(numBurgers, extraBurgers,burgerTotal):
    #global numBurgers
    #global burgerTotal
    #global extraBurgers
    numBurgers = int(input("How many Yum Yum Burgers would you like? "))
    questionBurgers = input("Is that all the Yum Yum Burgers you want?, y or n ")
    if questionBurgers == "y":
        extraBurgers = 0
        burgerTotal = numBurgers * 2.99
        return burgerTotal    
    else:
        extraBurgers = int(input("How many more would you like(or a negitive number to remove)? "))
        burgerTotal = (numBurgers + extraBurgers) * 2.99
        return burgerTotal
    print("You ordered {} Yum Yum Burgers" .format(numBurgers + extraBurgers))

2 个答案:

答案 0 :(得分:0)

您需要在定义函数时指定函数的参数:

def burgerChoice(numBurgers, burgerTotal, extraBurgers):

然后你需要在调用参数时传递参数:

burgerChoice(numBurgers, burgerTotal, extraBurgers)

答案 1 :(得分:0)

这个问题与'Python函数按引用调用'有关。

这是一个简短的例子:

(全球化)

def main():
    func_a();
    func_b();
    print 'main(): %d, %d' % (gv, id(gv))

def func_a():
    global gv;
    gv = 1
    print 'func_a(): %d, %d' % (gv, id(gv))

def func_b():
    print 'func_b(): %d, %d' % (gv, id(gv))  

(没有全球性)

def main():
    gv = None
    gv = func_a(gv)
    func_b(gv)
    print 'main(): %d, %d' % (gv, id(gv))

def func_a(av):
    av = 1
    print 'func_a(): %d, %d' % (av, id(av))
    return av

def func_b(bv):
    print 'func_b(): %d, %d' % (bv, id(bv))

顺便说一句,如果我们想让你的程序面向对象,这是另一个例子:

class example(object):

    def __init__(self):
        self.gv = None

    def main(self):
        self.func_a()
        self.func_b()
        print 'main(): %d, %d' % (self.gv, id(self.gv))

    def func_a(self):
        self.gv = 1
        print 'func_a(): %d, %d' % (self.gv, id(self.gv))

    def func_b(self):
        print 'func_b(): %d, %d' % (self.gv, id(self.gv))

ex = example()
ex.main()