簡體   English   中英

如何運行while循環並仍然接受用戶輸入?

[英]How can I run while loop and still accept user input?

def value(startH,startM,stopH,stopM):

        def job():
                do job 

        def job1():
                do another job


        start_time = "{0:02d}:{1:02d}".format(startH, startM)
        stop_time = "{0:02d}:{1:02d}".format(stopH, stopM)

        schedule.every().day.at(start_time).do(job)
        schedule.every().day.at(stop_time).do(job1)

        while True:
                schedule.run_pending()
                time.sleep(1)

在這里,startH,startM,stopH,stopM表示開始小時,開始分鍾,停止小時和停止分鍾。 這是用戶通過android提供的輸入。 此代碼運行。 它運行一次,然后繼續運行。 這是西裝。 如果我希望用戶再次輸入時間。 它不會接受。 當循環仍在運行時,如何接受用戶的輸入?僅說第一個任務說要打開燈,然后第二個任務要關掉燈。 因此,當第二項任務完成時。 假定已完成。我嘗試使用break,return。 它無法正常運行。

public void publish(int startH,int startM, int stopH, int stopM )
{
    JSONObject js = new JSONObject();
    try {
        js.put("START_HOUR", startH);
        js.put("START_MINUTE", startM);
        js.put("STOP_HOUR", stopH);
        js.put("STOP_MINUTE", stopM);



    }


public void setTime(View view)
{


    int storeStartHour = Integer.parseInt(startHrs.getText().toString());
    int storeStartMinutes = Integer.parseInt(startMinutes.getText().toString());
    int storeStopHour = Integer.parseInt(stopHrs.getText().toString());
    int storeStopMinutes = Integer.parseInt(stopMinutes.getText().toString());



     publish(storeStartHour, storeStartMinutes, storeStopHour, storeStopMinutes);



}

您可以使用線程

這是一個非常簡單的示例:

導入線程導入時間

def worker(num):
    # Do some stuff
    for i in range(5):
        time.sleep(2)
        print(2**(num + i))

if __name__ == "__main__":
    i = int(input("Enter a number: "))

    t = threading.Thread(target=worker, args=(i,)) # Always put a comma after the arguments. Even if you have only one arg.
    t.start() # Start the thread

    while True:
        choice = input()

        if choice == "stop":
            print("Waiting for the function to finish...")
            t.join() # Stop the thread (NOTE: the program will wait for the function to finish)
            break

        else:
            print(choice)

worker生成數字時,您仍然可以輸入內容。
除非確實需要,否則不要在worker函數中打印,因為stdout可能會變得混亂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM