通过比较播放器输入和列表和计时器未按预期工作的 If 语句

If statement by comparing player input and a list and timer not working as intended


wordlist = [['annoyed'], ['bulb'], ['fetch'], ['name'], ['noise'], ['wistful'], ['sparkle'], ['grain'], ['remind'], ['shocking'], ['productive'], ['superficial'], ['craven'], ['plate'], ['cup'], ['hat'], ['summer'], ['chilly'], ['crowd'], ['tiresome'], ['amount'], ['previous'], ['creepy'], ['insidious'], ['foolish'], ['trot'], ['well-groomed'], ['meat'], ['bottle'], ['van'], ['teeny-tiny'], ['edge'], ['knot'], ['disarm'], ['store'], ['shaggy'], ['furniture'], ['provide'], ['puzzled'], ['grubby'], ['texture'], ['cart'], ['tangy'], ['load'], ['stone'], ['plastic'], ['argument'], ['hop'], ['painstaking'], ['tense'], ['educate'], ['fearless'], ['fierce'], ['profuse'], ['addition'], ['staking'], ['attract'], ['boundary'], ['hurt'], ['delay'], ['tangible'], ['awesome'], ['ruthless'], ['guttural'], ['follow'], ['zephyr'], ['mute'], ['abandoned'], ['yak'], ['best'], ['continue'], ['stem'], ['cake'], ['multiply'], ['riddle'], ['delightful'], ['vulgar'], ['neck'], ['rampant'], ['complete'], ['certain'], ['plant'], ['organic'], ['reach'], ['tenuous'], ['chubby'], ['nut'], ['wiry'], ['knife'], ['first'], ['learned'], ['allow'], ['glass'], ['beef'], ['madly'], ['knowledgeable'], ['prepare'], ['compare'], ['perform'], ['rhetorical'], ['hover'], ['exciting'], ['adventurous'], ['cakes'], ['miniature'], ['deafening'], ['snail'], ['shy'], ['delirious'], ['hypnotic'], ['gigantic'], ['heady'], ['pen'], ['cent'], ['pump'], ['wide-eyed'], ['brief'], ['trains'], ['light'], ['order'], ['communicate'], ['bizarre'], ['flavor'], ['thirsty'], ['fasten'], ['black-and-white'], ['divergent'], ['gusty'], ['halting'], ['decide'], ['file'], ['ossified'], ['melt'], ['turkey'], ['avoid'], ['film'], ['null'], ['orange'], ['language'], ['adaptable'], ['cars'], ['eyes'], ['reject'], ['shave'], ['odd'], ['bruise'], ['cows'], ['curtain'], ['whirl'], ['wail'], ['deep'], ['mere'], ['grease'], ['phobic'], ['run'], ['scientific'], ['clear'], ['one'], ['wealthy'], ['pigs'], ['inquisitive'], ['toothsome'], ['memorise'], ['flap'], ['demonic'], ['cats'], ['injure'], ['jellyfish'], ['crow'], ['flame'], ['window'], ['rock'], ['chew'], ['pedal'], ['scared'], ['amuck'], ['hour'], ['wacky'], ['thoughtful'], ['used'], ['temporary'], ['fluttering'], ['pass'], ['ski'], ['zealous'], ['rhythm'], ['sea']]


#the word list is longer. shortened it for easier readability purposes. 

start = input("Press enter to start")
start_time = time.time()
time_limit = 10

start = input("Press enter to start")
while True:
    #timer function
    current_time = time.time()
    elapsed_time = current_time - start_time
    time_left = time_limit - elapsed_time

    #chooses a random word from list
    x = random.choice(wordlist)
    print(*x, "\n", sep = '')
    print(x)
    typed_word = input("type the word:")
    if typed_word == x:
        print("~correct~")
    else:
        print("~wrong~")

    if elapsed_time >= time_limit:
        print("time elapsed " + str(int(elapsed_time)))
        break

大家好,我刚刚开始了一个测试您的打字速度的新项目,但 运行 遇到了一些我似乎无法解决的问题。首先,计时器似乎超过了 10 秒的限制,如下图 url 所示。其次,我似乎无法通过程序验证玩家输入的单词。它总是输出 ~wrong~ 如下图所示 url。每次我从列表中打印一个单词时,它总是打印 [''] 这让游戏的美感看起来不愉快。所以,我首先认为问题是由于我使用 print(*x, "\n", sep = '') 删除了 [''] 所以,我尝试了替代输入,看看是否可以得到 typed_word == x。然而,如图url所示,它似乎是徒劳的。另外,我要求程序 print(x) 的原因是这样我可以准确验证从列表中提取的内容。拜托,我真的需要一些帮助!!!

输出图像:

https://ibb.co/n0KY70Y

需要更改代码以检查单词是否正确的两行是:

#input is by default a string. so you don't have to convert it to str()
typed_word = input("type the word:")

#wordlist is a list of list. 
#random.choice(wordlist) will give you a list from the list
#so you need to check typed_word against a list of length 1
if typed_word == x[0]:

这是完整的代码供您参考。我更改的唯一两行是上面的行,并设置了 start_timetime_limit

的值
import random
import time

wordlist = [['annoyed'], ['bulb'], ['fetch'], ['name'], ['noise'], ['wistful'], ['sparkle'], ['grain'], ['remind'], ['shocking'], ['productive'], ['superficial'], ['craven'], ['plate'], ['cup'], ['hat'], ['summer'], ['chilly'], ['crowd'], ['tiresome'], ['amount'], ['previous'], ['creepy'], ['insidious'], ['foolish'], ['trot'], ['well-groomed'], ['meat'], ['bottle'], ['van'], ['teeny-tiny'], ['edge'], ['knot'], ['disarm'], ['store'], ['shaggy'], ['furniture'], ['provide'], ['puzzled'], ['grubby'], ['texture'], ['cart'], ['tangy'], ['load'], ['stone'], ['plastic'], ['argument'], ['hop'], ['painstaking'], ['tense'], ['educate'], ['fearless'], ['fierce'], ['profuse'], ['addition'], ['staking'], ['attract'], ['boundary'], ['hurt'], ['delay'], ['tangible'], ['awesome'], ['ruthless'], ['guttural'], ['follow'], ['zephyr'], ['mute'], ['abandoned'], ['yak'], ['best'], ['continue'], ['stem'], ['cake'], ['multiply'], ['riddle'], ['delightful'], ['vulgar'], ['neck'], ['rampant'], ['complete'], ['certain'], ['plant'], ['organic'], ['reach'], ['tenuous'], ['chubby'], ['nut'], ['wiry'], ['knife'], ['first'], ['learned'], ['allow'], ['glass'], ['beef'], ['madly'], ['knowledgeable'], ['prepare'], ['compare'], ['perform'], ['rhetorical'], ['hover'], ['exciting'], ['adventurous'], ['cakes'], ['miniature'], ['deafening'], ['snail'], ['shy'], ['delirious'], ['hypnotic'], ['gigantic'], ['heady'], ['pen'], ['cent'], ['pump'], ['wide-eyed'], ['brief'], ['trains'], ['light'], ['order'], ['communicate'], ['bizarre'], ['flavor'], ['thirsty'], ['fasten'], ['black-and-white'], ['divergent'], ['gusty'], ['halting'], ['decide'], ['file'], ['ossified'], ['melt'], ['turkey'], ['avoid'], ['film'], ['null'], ['orange'], ['language'], ['adaptable'], ['cars'], ['eyes'], ['reject'], ['shave'], ['odd'], ['bruise'], ['cows'], ['curtain'], ['whirl'], ['wail'], ['deep'], ['mere'], ['grease'], ['phobic'], ['run'], ['scientific'], ['clear'], ['one'], ['wealthy'], ['pigs'], ['inquisitive'], ['toothsome'], ['memorise'], ['flap'], ['demonic'], ['cats'], ['injure'], ['jellyfish'], ['crow'], ['flame'], ['window'], ['rock'], ['chew'], ['pedal'], ['scared'], ['amuck'], ['hour'], ['wacky'], ['thoughtful'], ['used'], ['temporary'], ['fluttering'], ['pass'], ['ski'], ['zealous'], ['rhythm'], ['sea']]


#the word list is longer. shortened it for easier readability purposes. 

start = input("Press enter to start")

#I started the clock here as soon as you press the enter key
start_time = time.time()

#i set the time to 10.0 seconds
time_limit = 10.0

while True:
    #timer function
    current_time = time.time()
    elapsed_time = current_time - start_time
    time_left = time_limit - elapsed_time

    #choses a random word from list
    x = random.choice(wordlist)
    print(*x, "\n", sep = '')
    print(x)
    typed_word = input("type the word:")
    if typed_word == x[0]:
        print("~correct~")
    else:
        print("~wrong~")

    if elapsed_time >= time_limit:
        print("time elapsed " + str(int(elapsed_time)))
        break

输出为:

Press enter to start
pedal

['pedal']
type the word:pedal
~correct~
amuck

['amuck']
type the word:pedal
~wrong~
stone

['stone']
type the word:amuck
~wrong~
chubby

['chubby']
type the word:chubby
~correct~
rhythm

['rhythm']
type the word:rhythm
~correct~
nut

['nut']
type the word:nut
~correct~
time elapsed 14