Python Forum

Full Version: how to get the value out the def function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Error:
Error : print(choice_cost + car_cost) NameError: name 'choice_cost' is not defined
thank you

"""Total Car Cost"""

car_cost = 500

def total_cost():
    print(choice_cost + car_cost)


def color_cost():
    print()
    print("For Blue Enter 1.","For Red Enter 2" )
    print()
    choice = int(input("Please enter color choice :"))
    if choice == 1:
        choice_cost = 100
        total_cost()
    elif choice == 2:
        choice_cost = 200
        total_cost()
    else:
        print("Invalid Choice. Please choose again")
    color_cost()

color_cost()
Use return.
Example
def some_function(input_value):
    what_to_return = input_value + 100
    return what_to_return

returned_value = some_function(10)
print(f'Returned value: {returned_value}')
Output:
Returned value: 110
Thank you so much