如何从def语句上方获取变量并将其在def语句中使用?

时间:2019-03-26 20:21:15

标签: python python-3.x

如何从def语句上方获取变量并将其在def语句中使用?

total_cost = 0 

def main():

    A = input ( "   Big Mac Bacon = a1 \n   The BBQ and Bacon Chicken - wrap = a2 \n   The Fiery Buffalo Chicken One - wrap = a3 \n   Chicken Legend with Cool Mayo = a4 \n Choose a main meal: ")
    a1 = float (3.09) # Cost of The Big Mac Bacon
    a2 = float (4.59) # Cost of The BBQ and Bacon Chicken
    a3 = float (4.59) # Cost of The Fiery Buffalo Chicken One - wrap
    a4 = float (4.99) # Cost of Chicken Legend with Cool Mayo

    if A == "a1": # If this option is selected, ths price will be added
        total_cost = total_cost + a1# This option is selected, ths price will be added
        side()
    elif A == "a2": # If this option is selected, ths price will be added
        total_cost = total_cost + a2# This option is selected, ths price will be added
        side()
    elif A == "a3": # If this option is selected, ths price will be added
        total_cost = total_cost + a3# This option is selected, ths price will be added
        side()
    elif A == "b4":# If this option is selected, ths price will be added
        total_cost = total_cost + a4 # This option is selected, ths price will be added
        side()
    else:
        while A != "a1" or "a2" or "a3" or "a4":
            print ( "Please enter a valid answer" )
            A = input ( "   Big Mac Bacon = a1 \n   The BBQ and Bacon Chicken - wrap = a2 \n   The Fiery Buffalo Chicken One - wrap = a3 \n   Chicken Legend with Cool Mayo = a4 \n Choose a main meal: ")
            main()
    return total_cost

0 个答案:

没有答案