Làm thế nào để bạn giới hạn thời gian đầu vào trong python?

timedInteger[] và timedFloat hoạt động giống như timedInput[], ngoại trừ chúng chỉ cho phép người dùng nhập số và dấu phẩy hoặc dấu chấm trong trường hợp timedFloat. Có thể dùng để nhập số âm

def timedInteger[prompt="", timeout=5, resetOnInput=True, allowNegative=True]

Hàm timedInteger[] và timedFloat[] từ pytimedinput chấp nhận các tham số sau

  • dấu nhắc, str. một chuỗi để hiển thị cho người dùng dưới dạng lời nhắc khi chờ đầu vào

    Mặc định là một chuỗi rỗng

  • thời gian chờ, int. phải đợi bao nhiêu giây trước khi hết thời gian

    Mặc định là 5 giây, sử dụng -1 để tắt

  • đặt lạiOnInput, bool. Đặt lại bộ đếm thời gian chờ bất cứ khi nào người dùng nhấn một phím

    Mặc định là True

  • allowNegative, bool. Có cho phép người dùng nhập giá trị âm hay không

Hàm trả về một bộ giá trị

  • int/float hoặc Không. một số nguyên hoặc số float, tùy thuộc vào chức năng nào được gọi hoặc Không có, nếu không có số nào được nhập

    Tôi đã cố gắng tìm một mã giới hạn thời gian đầu vào tốt cho các tập lệnh python và cuối cùng tôi đã có một mã để hoạt động

    from threading import Timer timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[]

    nhưng, tôi cần có thể chạy một chức năng khi bộ hẹn giờ kết thúc.
    đồng thời - tôi muốn hàm được gọi bên trong mã hẹn giờ - nếu không, nếu bạn nhập mục nhập trước khi hết giờ, thì hàm vẫn sẽ được gọi cho dù thế nào đi chăng nữa.
    Có ai vui lòng chỉnh sửa mã này không? Tôi phải có thể chạy một chức năng khi bộ hẹn giờ kết thúc? .

    2 năm trước

    sau đó chỉ cần gọi chức năng?

    from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

    2 năm trước

    @ Coder100 Vấn đề là, bộ đếm thời gian nằm bên trong chức năng mà nó đang gọi. Và các mã khác cần được thực thi theo loại thứ tự riêng của nó. Tôi có thể cấp cho bạn quyền truy cập vào mã mà tôi đang làm. điều đó có lẽ sẽ có ý nghĩa hơn

    2 năm trước

    import time mint = input["How many seconds you want to time?:"] timer = int[mint] while [timer != 0 ]: timer=timer-1 time.sleep[1] print[timer]

    Bạn có thể sửa đổi mã này và thêm các câu lệnh if tùy thích

    2 năm trước

    @abdullahrajput9 Tôi muốn đồng hồ hẹn giờ không hiển thị cho đến khi đồng hồ hẹn giờ kết thúc. Ngoài ra- tôi muốn mã chỉ cung cấp cho bạn một khoảng thời gian giới hạn để nhập thông tin đầu vào

    Tôi đã tự hỏi làm thế nào tôi có thể tạo một chương trình với đầu vào TỐI ĐA 5 giây[e. g anh ấy có thể gửi đầu vào sau 2 giây] bằng python Tôi quyết định thực hiện một trò chơi ĐƠN GIẢN trong đó về cơ bản bạn phải viết lại một từ dưới 5 giây. Tôi biết cách tạo đầu vào và đợi nó CHÍNH XÁC 5 GIÂY, nhưng điều tôi muốn đạt được là đặt thời gian nhập tối đa là 5 giây để nếu người dùng nhập câu trả lời trong giả sử 2 giây, anh ta sẽ chuyển sang từ tiếp theo. bạn có thể cho tôi biết cách để đạt được mục tiêu của tôi. Cảm ơn trước. [Tôi có thể trả lời vào ngày hôm sau trong trường hợp nhận được câu trả lời vì tôi phải đi ngủ]

    Mã tôi có ngay bây giờ

    for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
    
        # User gets maximum of 5 seconds to write the word,
        # if he does it before 5 seconds pass ,he goes to next word [does not have to wait exactly 5 seconds, he   
        # can send input in e.g 2 seconds]      
        # if he does not do it in 5 seconds he loses game and it is finished
    
    
        user_input = input[f"Type word '{word}': "]
    
        #IF the word is correct go to next iteration
        if[user_input==word]:
            continue
    
        #If the word is incorrect finish the game
        else:
            print["You lost"]
            break
    
    Tôi đã thử làm điều đó với luồng. Timer[] nhưng nó không hoạt động
    import threading
    
    class NoTime[Exception]:
        pass
    
    def count_time[]:
        raise NoTime
    
    for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
        try:
    
    
            #Create timer which raises exception after 5 seconds
            timer = threading.Timer[5,count_time]
            timer.start[]
    
            user_input = input[f"Type word '{word}': "]
            #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
            timer.cancel[]
    
            if user_input==word:
                print["Correct"]
            else:
                print["Incorrect, you LOSE!"]
                break
    
        except NoTime:
            print["You run out of time, you lose"]
            break
    
    
    Tôi gặp lỗi
    Traceback [most recent call last]:
      File "C:\Users\papit\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
        self.run[]
      File "C:\Users\papit\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1394, in run
        self.function[*self.args, **self.kwargs]
      File "C:\Users\papit\OneDrive\Pulpit\Programming\Python Bro Course\Math\second\threading_training.py", line 7, in count_time
        raise NoTime
    NoTime
    

    Tôi đã tự hỏi làm cách nào để tạo một chương trình với đầu vào TỐI ĐA 5
    giây[e. g anh ấy có thể gửi đầu vào sau 2 giây] bằng python Tôi quyết định
    làm một trò chơi ĐƠN GIẢN trong đó về cơ bản bạn phải viết lại một từ dưới 5
    giây. Tôi biết cách tạo dữ liệu đầu vào và đợi nó CHÍNH XÁC 5 GIÂY,

    Bạn có?

    nhưng điều tôi muốn đạt được là đặt thời gian nhập tối đa là 5 giây
    vì vậy nếu người dùng nhập câu trả lời sau 2 giây, người đó sẽ chuyển sang bước tiếp theo
    word. Could you tell me the way to achieve my goal. Thanks in
    trước.
    [Tôi có thể trả lời vào ngày hôm sau trong trường hợp nhận được câu trả lời vì tôi
    phải đi ngủ]

    Không sao đâu, tất cả chúng ta ở đây có múi giờ khác nhau

    Mã tôi có ngay bây giờ

     import threading
    
     class NoTime[Exception]:
         pass
    
     def count_time[]:
         raise NoTime
    
     for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
         try:
             #Create timer which raises exception after 5 seconds
             timer = threading.Timer[5,count_time]
             timer.start[]
             user_input = input[f"Type word '{word}': "]
             #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
             timer.cancel[]
    

    Tất cả điều này là tốt cho đến nay. Ngoại trừ

    from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

    0/

    from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

    1

    ________số 8

    Điều khó khăn là

    from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

    2 chạy trong một

    from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

    3 riêng biệt, vì vậy khi
    nó phát sinh một ngoại lệ thì ngoại lệ đó sẽ giải phóng ngăn xếp của

    from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

    3
    chứ không phải .

    Lỗi tôi nhận được_______9

    Thấy rằng trong lần theo dõi này, chương trình chính của bạn không phải là một phần của lệnh gọi
    ngăn xếp. Thay vào đó,

    import threading
    
    class NoTime[Exception]:
        pass
    
    def count_time[]:
        raise NoTime
    
    for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
        try:
    
    
            #Create timer which raises exception after 5 seconds
            timer = threading.Timer[5,count_time]
            timer.start[]
    
            user_input = input[f"Type word '{word}': "]
            #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
            timer.cancel[]
    
            if user_input==word:
                print["Correct"]
            else:
                print["Incorrect, you LOSE!"]
                break
    
        except NoTime:
            print["You run out of time, you lose"]
            break
    
    
    1 được gọi trực tiếp từ
    thư viện luồng. Đây là đặc điểm của mã được chạy trong luồng riêng của nó.

    Về cơ bản, bạn không thể làm gián đoạn một

    from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

    3. Điều tốt nhất bạn có thể làm là
    đặt các biến và thăm dò chúng hoặc chạy các thứ song song trong Chủ đề và
    thu thập kết quả của chúng.

    Những gì bạn có thể làm là

    • làm một
      import threading
      
      class NoTime[Exception]:
          pass
      
      def count_time[]:
          raise NoTime
      
      for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
          try:
      
      
              #Create timer which raises exception after 5 seconds
              timer = threading.Timer[5,count_time]
              timer.start[]
      
              user_input = input[f"Type word '{word}': "]
              #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
              timer.cancel[]
      
              if user_input==word:
                  print["Correct"]
              else:
                  print["Incorrect, you LOSE!"]
                  break
      
          except NoTime:
              print["You run out of time, you lose"]
              break
      
      
      3
    • chạy
      import threading
      
      class NoTime[Exception]:
          pass
      
      def count_time[]:
          raise NoTime
      
      for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
          try:
      
      
              #Create timer which raises exception after 5 seconds
              timer = threading.Timer[5,count_time]
              timer.start[]
      
              user_input = input[f"Type word '{word}': "]
              #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
              timer.cancel[]
      
              if user_input==word:
                  print["Correct"]
              else:
                  print["Incorrect, you LOSE!"]
                  break
      
          except NoTime:
              print["You run out of time, you lose"]
              break
      
      
      4 trong Chủ đề riêng của nó và yêu cầu
      import threading
      
      class NoTime[Exception]:
          pass
      
      def count_time[]:
          raise NoTime
      
      for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
          try:
      
      
              #Create timer which raises exception after 5 seconds
              timer = threading.Timer[5,count_time]
              timer.start[]
      
              user_input = input[f"Type word '{word}': "]
              #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
              timer.cancel[]
      
              if user_input==word:
                  print["Correct"]
              else:
                  print["Incorrect, you LOSE!"]
                  break
      
          except NoTime:
              print["You run out of time, you lose"]
              break
      
      
      5 đầu vào
      kết quả cho Hàng đợi
    • khởi động một

      from threading import Timer def done[]: pass # .. add your code here timeout = 5 t = Timer[timeout, print, ["Time's up!"]] t.start[] entry = input['> '] t.cancel[] done[]

      2 để đặt một giá trị đặc biệt, chẳng hạn như
      import threading
      
      class NoTime[Exception]:
          pass
      
      def count_time[]:
          raise NoTime
      
      for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
          try:
      
      
              #Create timer which raises exception after 5 seconds
              timer = threading.Timer[5,count_time]
              timer.start[]
      
              user_input = input[f"Type word '{word}': "]
              #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
              timer.cancel[]
      
              if user_input==word:
                  print["Correct"]
              else:
                  print["Incorrect, you LOSE!"]
                  break
      
          except NoTime:
              print["You run out of time, you lose"]
              break
      
      
      7 vào
      import threading
      
      class NoTime[Exception]:
          pass
      
      def count_time[]:
          raise NoTime
      
      for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
          try:
      
      
              #Create timer which raises exception after 5 seconds
              timer = threading.Timer[5,count_time]
              timer.start[]
      
              user_input = input[f"Type word '{word}': "]
              #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
              timer.cancel[]
      
              if user_input==word:
                  print["Correct"]
              else:
                  print["Incorrect, you LOSE!"]
                  break
      
          except NoTime:
              print["You run out of time, you lose"]
              break
      
      
      3

    Chương trình chính đọc từ hàng đợi. tùy điều kiện nào xảy ra trước [

    import threading
    
    class NoTime[Exception]:
        pass
    
    def count_time[]:
        raise NoTime
    
    for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
        try:
    
    
            #Create timer which raises exception after 5 seconds
            timer = threading.Timer[5,count_time]
            timer.start[]
    
            user_input = input[f"Type word '{word}': "]
            #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
            timer.cancel[]
    
            if user_input==word:
                print["Correct"]
            else:
                print["Incorrect, you LOSE!"]
                break
    
        except NoTime:
            print["You run out of time, you lose"]
            break
    
    
    9 hoặc bộ đếm thời gian] sẽ là giá trị đầu tiên bạn
    Traceback [most recent call last]:
      File "C:\Users\papit\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1038, in _bootstrap_inner
        self.run[]
      File "C:\Users\papit\AppData\Local\Programs\Python\Python311\Lib\threading.py", line 1394, in run
        self.function[*self.args, **self.kwargs]
      File "C:\Users\papit\OneDrive\Pulpit\Programming\Python Bro Course\Math\second\threading_training.py", line 7, in count_time
        raise NoTime
    NoTime
    
    0 từ
    import threading
    
    class NoTime[Exception]:
        pass
    
    def count_time[]:
        raise NoTime
    
    for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
        try:
    
    
            #Create timer which raises exception after 5 seconds
            timer = threading.Timer[5,count_time]
            timer.start[]
    
            user_input = input[f"Type word '{word}': "]
            #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
            timer.cancel[]
    
            if user_input==word:
                print["Correct"]
            else:
                print["Incorrect, you LOSE!"]
                break
    
        except NoTime:
            print["You run out of time, you lose"]
            break
    
    
    3. Hủy hẹn giờ. Kiểm tra giá trị.

    Điều này vẫn để lại việc chặn

    import threading
    
    class NoTime[Exception]:
        pass
    
    def count_time[]:
        raise NoTime
    
    for word in ["banana","earth","turtle","manchester","coctail","chicken"]:
        try:
    
    
            #Create timer which raises exception after 5 seconds
            timer = threading.Timer[5,count_time]
            timer.start[]
    
            user_input = input[f"Type word '{word}': "]
            #if timer hasn't lasted 5 seconds then destroy it in order to prevent unwanted exception
            timer.cancel[]
    
            if user_input==word:
                print["Correct"]
            else:
                print["Incorrect, you LOSE!"]
                break
    
        except NoTime:
            print["You run out of time, you lose"]
            break
    
    
    9, đây có thể là vấn đề đối với
    nhập thêm.

    Chúc mừng!
    Cameron Simpson cs@cskk. Tôi. au

Chủ Đề