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 đã cố gắng làm điều đó với luồng. Timer[] nhưng nó không hoạt động_______1_______Tôi gặp lỗi_______2_______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] trong 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 đầu vào và đợi 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 liệu 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, anh ta sẽ chuyển sang bước tiếp theo
từ. bạn có thể cho tôi biết cách để đạt được mục tiêu của tôi. cảm ơn trong
nâng cao
[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ừ try
/_______17_______
if user_input==word:
print["Correct"]
else:
print["Incorrect, you LOSE!"]
break
except NoTime:
print["You run out of time, you lose"]
break
Khó khăn là Timer
chạy trong một Thread
riêng biệt, vì vậy khi
nó đưa ra một ngoại lệ mà ngoại lệ đó giải phóng các Thread
riêng biệt
ngăn xếp, không phải ngăn xếp chương trình chính của bạn
Lỗi tôi nhận được_______5_______
Thấy rằng trong truy nguyên này, chương trình chính của bạn không phải là một phần của cuộc gọi
cây rơm. 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ừ luồngthư viện. Đâ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 Thread
. Đ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 những thứ song song trong Chủ đề và
thu thập kết quả của họ
Những gì bạn có thể làm là
- làm một
3import 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
- chạy
4 trong Chủ đề của chính nó và đặt đầu vào là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
5import 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
kết quả vào hàng đợi - bắt đầu một
Timer
để đặt một giá trị đặc biệt, chẳng hạn như
7 vàoimport 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
3import 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
Chương trình chính đọc từ hàng đợi. tùy theo điều kiện nào xảy ra trước [cá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à một vấn đề đối vớiđầu vào thêm
Chúc mừng,
Cameron Simpson cs@cskk. nhận dạng. âu