omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Calling one function in another function

    Pythonista
    functions loop python 3
    2
    2
    1475
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • craigmrock
      craigmrock last edited by

      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()
      
      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        You want to use the global keyword 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.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB Forums | Contributors