Calling one function in another function
Trying to store my calculations in one function and call it in my input function to output a total. If level 1 is selected, I want it to find level 1 in the get_sales function and multiply that percentage by the salesAmount variable in the salesPerson function.
def get_sales(s): level1 = 0 if s <= 2500: level1 = s * 0.045 return level1 def salesPerson(): salesNum = input("How many sales persons? ") for num in salesNum: salesName = input("Enter sales person name: ") salesLevel = 0 while salesLevel == 0: try: salesLevel = int(input("Enter sales person level: ")) if salesLevel < 1 or salesLevel > 4: print("error, try again.") salesLevel = int(input("Enter sales person level: ")) if salesLevel == 1: salesLevel = get_sales(salesLevel) except: print("error, try again.") continue hoursWorked = float(input("Enter hours worked: ")) salesAmount = float(input("Enter sales amount: ")) print(salesLevel*salesAmount) salesPerson()
You want to use the
globalkeyword inside your functions in order to allow your variable to be modified outside the defining scope.
Actually you think you want globals,but usually they lead to grief, since it can be hard to debug -- for instance when you forget to declare global inside a function, you can read variables defined in external scope but not modify it. So it is easy to get confused.
Consider using a class, then you can store things like that as class instance variables. Then it is never ambiguous which copy of the variable you are modifying.