python-3.x 当计时器用完时,猜测游戏不会停止

bzzcjhmw  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(107)

我做了一个小美国州猜谜游戏,你有60秒的时间来猜测尽可能多的州,然后脚本会更新你的最高分。然而,我有一个计时器的问题。它完美地工作,除了当它用完时,你仍然可以在它退出代码之前输入另一个答案。任何想法?
我也有一个问题,我希望用户被提醒多久,他们已经离开.但我的45,30,15,10和5秒的警告得到的方式,用户的输入,但一个问题的时间,我猜。
有什么想法吗?

import time
import threading

# make list of 50 states
states = [x.lower() for x in ["Alaska", "Alabama", "Arkansas", "American Samoa", "Arizona", "California", "Colorado",
                              "Connecticut", "District ", "of Columbia", "Delaware", "Florida", "Georgia", "Guam",
                              "Hawaii",
                              "Iowa", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana",
                              "Massachusetts",
                              "Maryland", "Maine", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana",
                              "North Carolina", "North Dakota", "Nebraska", "New Hampshire", "New Jersey", "New Mexico",
                              "Nevada", "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Puerto Rico",
                              "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",
                              "Virginia",
                              "Virgin Islands", "Vermont", "Washington", "Wisconsin", "West Virginia", "Wyoming"]]

# start the game
print("Welcome to the US states game!\nYou will have 60 seconds to name as many states as possible\nPress enter to "
      "start!")
input()
print("starting game!")

time_up = False
score = 0

# start counting down from 60
def countdown():
    global time_up
    for seconds in range(10, 0, -1):
        time.sleep(1)

        # 45, 30, 15, 10, and 5 second warnings
        if seconds in (45, 30, 15, 10, 5):
            print(f"\n{seconds} seconds left!")

    time_up = True

# take answers as inputs
def guessing_game():
    global score
    while not time_up:
        guess = input("Guess a state: ")
        guess = guess.lower()
        if guess in states:
            score += 1
            print(f"Correct! score is now {score}")
        else:
            print(f"{guess} is not a state! try again")

    print(f"Time's up, final score is {score}")

# create threads for countdown timer and guessing game
countdown_thread = threading.Thread(target=countdown)
game_thread = threading.Thread(target=guessing_game)

# start both threads
countdown_thread.start()
game_thread.start()

# wait for both threads to finish
countdown_thread.join()
game_thread.join()

# once finished write/rewrite high-score on a txt file

'''
countdown timer gets in the way of answering

game does not finish after countdown, it lets you guess one more time

need function to write/rewrite high score on text file

need to get rid of US overseas territories from list
'''

字符串
我尝试在while循环中放入另一个break语句,但没有成功

zpjtge22

zpjtge221#

你可以用这个:

from threading import Timer

def timeout():
    # do your stuff here

t = Timer(number_of_seconds, timeout)
t.start()

字符串

相关问题