Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
63 views
in Technique[技术] by (71.8m points)

python - Why does it still say 0 although I am adding on to it?

READ THIS FIRST BEFORE

  • Firstly, allow me to explain what you should input to get the mistake.
    • Run the program.
    • Name your company to anything.
    • Then see company stats by clicking 1.
    • After that work on your company by clicking 2
    • Then hire people by clicking 1
    • Hire maybe two or three people by clicking the respective numbers.
    • Then click 6 and go back.
    • Then see the company stats again.
    • PROBLEM HERE. The number of employees remain the same

WHY?

What I think I did ?

  • My problem is in the function employees() on line 24.
  • Although I do employee_num += 1, the number of employees remain the same.

This is my question. Please tell me how to fix that and also provide a reason.

CODE:


from colored import fg
import random

# Colors
green = fg("green")
white = fg("white")
yellow = fg("yellow")
blue = fg("blue")
magenta = fg("magenta")
red = fg('red')
wheat_4 = fg("wheat_4")

# Main Assets
nums_3 = ['1', '2', '3']
nums_6 = ['1', '2', '3', '4', '5', '6']
revenue = 0
products = 0
employee_num = 0

# Start with company's name
company_name = input(red + "What would you like to name your company: ")

def company():
    def employees():
        employee_num = 0
        # getting file data
        employees_open = open('employees.txt', 'r')
        employees_open_read = employees_open.readlines()
        employees_open_read_fixed = [line.strip() for line in employees_open_read]
        employees_open = employees_open.close()

        #Employee names
        random_employee1 = random.choice(employees_open_read_fixed)
        random_employee2 = random.choice(employees_open_read_fixed) 
        random_employee3 = random.choice(employees_open_read_fixed) 

        # Salaries
        salaries = [10000, 15000, 20000, 30000, 40000, 50000, 60000, 70000, 80000]
        employee_salary1 = random.choice(salaries)
        employee_salary2 = random.choice(salaries)
        employee_salary3 = random.choice(salaries)

        #Names in a list
        salary_payed = 0

        while True:
            hiring = input(f"{yellow}
Here is a list of people you can hire:

         [1] Name: {random_employee1} | Salary: ${employee_salary1}
         [2] Name: {random_employee2} | Salary: ${employee_salary2}
         [3] Name: {random_employee3} | Salary: ${employee_salary3}
         [4] Or go back to main page. 

Enter the number of the person you would like to hire: ---> ")
            while hiring in nums_3:
                if hiring == '1':
                    salary_payed += employee_salary1
                    random_employee1 = random.choice(employees_open_read_fixed)
                    employee_num += 1
                    break

                if hiring == '2':
                    salary_payed += employee_salary2
                    random_employee2 = random.choice(employees_open_read_fixed)
                    employee_num += 1
                    break

                if hiring == '3':
                    salary_payed += employee_salary3
                    random_employee3 = random.choice(employees_open_read_fixed)
                    employee_num += 1
                    break


            if hiring == '4':
                break



    while True:
        first_choice = input(magenta + "
What do you want to do:

         [1] See your company's stats
         [2] Work on your company.
---> ")

        while first_choice in nums_3:
            if first_choice == '1':
                print(f"{company_name.title()}:
{green}Revenue: {revenue}
Employees: {employee_num}
Products Sold: {products}")
                break

            if first_choice == '2':
                company_work = input(wheat_4 + "Here is what you can do with your company:

         [1] Hire new people
         [2] Build a phone.
         [3]Buy Shares
         [4]Sell shares
         [5]Sell your products
         [6] Or go back.
---> ")

                if company_work == '6':
                    break

                while company_work in nums_3:
                    if company_work == '1':
                        employees()
                        break

                


company()

question from:https://stackoverflow.com/questions/65902153/why-does-it-still-say-0-although-i-am-adding-on-to-it

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want to modify a variable in a function other than the place that variable exists, you need either a global or nonlocal statement, depending on whether your variable is global or not. Your employee_num is global, so you should put

global employee_num

as the first line of any function which will modify it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...