【问题标题】:Limiting time.sleep() function to one loop in python将 time.sleep() 函数限制为 python 中的一个循环
【发布时间】:2019-03-03 16:14:37
【问题描述】:

我正在创建一个银行界面,我希望每 10 秒应用一次年度利息(作为修改后的时间尺度的一部分)。

import time

customers = 100
savings = []
fixdeposit = []

ask_user_type = """
What is your user type?
(type 1 if you're an employee)
(type 2 if you're a customer)
"""

employee_function_options = """
What function would you like to carry out?
(type 1 for total savings value)
(type 2 for a specific savings value)
(type 3 for total fixed deposit value)
(type 4 for a specific fd's value)
(type 5 to add a customer)
(type 6 to remove a customer)
(type 7 to end program)
"""

customer_function_options = """
What function would you like to carry out?
(type 1 for withdrawal)
(type 2 for deposit)
(type 3 for savings account balance)
(type 4 to create fixed deposit)
(type 5 to end fixed deposit)
(type 6 for fixed deposit value)
(type 7 to end program)
"""

for i in range (100):
    savings.append(1000)
    fixdeposit.append(0)

while 1 == 1:
    time.sleep(10)
    savings = [i * 1.04 for i in savings]
    fixdeposit = [i * 1.07 for i in fixdeposit]

user = int(input(ask_user_type))

while 1==1:
    if user == 1:
        function = int(input(employee_function_options))
        if function == 1:
            print (sum(savings))
            function = int(input(employee_function_options))

        elif function == 2:
            saving_account = int(input("What is the chosen account's number?"))
            print (savings[saving_account])
            function = int(input(employee_function_options))

        elif function == 3:
            print (sum(fixdeposit))
            function = int(input(employee_function_options))

        elif function == 4:
            fd_no = int(input("What is the chosen fd's number?"))
            print(fixdeposit[fd_no])
            function = int(input(employee_function_options))

        elif function == 5:
            no_new_customers = int(input("How many new customers do you want to add?"))
            for i in range(no_new_customers):
                savings.append(1000)
                fixdeposit.append(0)
            print ("Task Completed")
            function = int(input(employee_function_options))

        elif function == 6:
            account_deleted = int(input("What is the number of the account to be deleted?"))
            savings[account_deleted] = 0
            fixdeposit[account_deleted] = 0
            print ("Task Completed")
            function = int(input(employee_function_options))

        elif function == 7:
            print ("program ended")
            user = int(input(ask_user_type))

        else:
            print("Error")
            function = int(input(employee_function_options))


    elif user == 2:
        function = int(input(customer_function_options))
        if function == 1:
            account_no = int(input("What is your account number?"))
            withdrawal_amount = float(input("How much money do you want to withdraw?"))
            savings[account_no] = savings[account_no] - withdrawal_amount
            withdrawal_amount = 0
            function = int(input(customer_function_options))

        elif function == 2:
            account_no = int(input("What is your account number?"))
            deposit_amount = float(input("How much money do you want to deposit?"))
            savings[account_no] = savings[account_no] + deposit_amount
            deposit_amount = 0
            function = int(input(customer_function_options))

        elif function == 3:
            account_no = int(input("What is your account number?"))
            print(savings[account_no])
            function = int(input(customer_function_options))

        elif function == 4:
            account_no = int(input("What is your account number?"))
            fd_amount = float(input("How much money do you want in your fd?"))
            fixdeposit[account_no] = fd_amount
            fd_amount = 0
            function = int(input(customer_function_options))

        elif function == 5:
            account_no = int(input("What is your account number?"))
            savings[account_no] = savings[account_no] + fixdeposit[account_no]
            fixdeposit[account_no] = 0
            function = int(input(customer_function_options))

        elif function == 6:
            account_no = int(input("What is your account number?"))
            print(fixdeposit[account_no])
            function = int(input(customer_function_options))

        elif function == 7:
            user = int(input(ask_user_type))

        else:
            print("Error")
            function = int(input(customer_function_options))


    else:
        print("Error")
        user = int(input(ask_user_type))

我使用了time.sleep(),但在我看来它也阻止了用户界面的工作。

有没有人知道任何解决方法或方法将time.sleep() 函数限制为感兴趣的循环?

【问题讨论】:

    标签: python loops delay


    【解决方案1】:

    尝试使用threading,将更新savingsfixdeposit 的部分移动到另一个线程。这是一个有效的例子,我只截断了一些没有改变的代码部分,你会看到updating 每 10 秒打印一次:

    import threading
    import time
    
    customers = 100
    savings = []
    fixdeposit = []
    
    ask_user_type = ...
    
    employee_function_options = ...
    
    customer_function_options = ...
    
    for i in range (100):
        savings.append(1000)
        fixdeposit.append(0)
    
    
    def regular_update():
        global savings, fixdeposit
        while True:
            time.sleep(10)
            print('updating')
            savings = [i * 1.04 for i in savings]
            fixdeposit = [i * 1.07 for i in fixdeposit]
    
    
    my_thread = threading.Thread(target=regular_update, args=())
    my_thread.start()
    
    user = int(input(ask_user_type))
    ... everything here as in your code
    

    【讨论】:

    • 不是每次都更新整个 fixdeposit 数组,是否可以将兴趣应用于数组中的一个特定元素(取决于想要创建 f.d. 的用户的 account_no)。有什么不同的线程吗?我不知道它们是如何工作的,所以我真的需要一些帮助......谢谢。
    • 要更新一个特定的元素,你可以写fixdeposit[account_no] = fixdeposit[account_no] * 1.07。您可以在regular_update 线程中执行此操作或创建另一个线程。
    【解决方案2】:

    您的 while 循环永远不会中断以允许程序的其他部分运行。

    【讨论】:

      【解决方案3】:
      while 1 == 1:
          time.sleep(10)
          savings = [i * 1.04 for i in savings]
          fixdeposit = [i * 1.07 for i in fixdeposit]
      

      由于 while 循环中的条件 1 == 1,它会永远执行,因为程序无法在内部中断循环。这意味着您的time.sleep(10) 将无限期运行。


      更新: 假设您想要用户的 有效 输入(取决于您定义为有效的内容),您可以执行以下操作之一:

      number = (int)(input("Enter a number: "))
      
      # 3 is just chosen here as a representative value
      # The loop will run until 3 is received as input
      while number != 3: 
        number = (int)(input("Enter a number: "))
      
      print("Exited loop")
      

      或者您可以使用break 语句在满足条件时退出循环:

      # 3 is just chosen here as a representative value
      # The loop will run indefinitely, but if 3 is received as input, it will break the loop (force it to exit)
      while 1 == 1:
        number = (int)(input("Enter a number: "))
        if number == 3:
          break
      
      print("Exited loop")
      

      【讨论】:

      • 如何在接收到用户输入时对其进行编程以打破循环......这可能吗?
      • @MananAgarwal 检查我上面的更新答案。如果它对您有用,请不要忘记检查答案。
      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 2019-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多