0

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 秒的限制,如下图所示。其次,我似乎无法对玩家输入的单词进行编程验证。它总是输出〜错误〜,如下图所示。每次我从列表中打印一个单词时,它总是带有 [''] ,这使得游戏的美学看起来令人不快。所以,我首先认为问题是由于我使用print(*x, "\n", sep = '') so 删除了 [''] ,我尝试了其他输入以查看是否可以得到typed_word == x. 但是,如图片网址所示,这似乎是徒劳的。另外,我要求程序打印(x)的原因是我可以准确地验证从列表中提取的内容。

输出图像:

https://ibb.co/n0KY70Y

4

1 回答 1

1

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

#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
于 2020-08-10T04:39:52.463 回答