【发布时间】:2020-11-29 16:45:59
【问题描述】:
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 = '') 删除了 ['']
所以,我尝试了其他输入,看看是否可以得到typed_word == x。但是,如图片网址所示,这似乎是徒劳的。另外我要求程序打印(x)的原因是我可以准确地验证从列表中提取的内容。请问我真的需要一些帮助!!!
输出图像:
【问题讨论】:
-
if typed_word == x:这种比较永远不会成立,因为typed_word是一个普通字符串,而x是一个包含字符串的列表,例如['annoyed'].列表永远不能等于字符串。 -
将
wordlist更改为字符串列表。现在它是一个列表列表,其中每个内部列表都包含一个字符串(这不是很有用)。 -
您应该检查
if typed_word == x[0]:,因为x将始终是一个列表。您正在使用随机函数x = random.choice(wordlist)从列表中选择一个列表。如果您的列表只是一个字符串列表而不是列表列表,那么您可以检查x。在这里,代码必须是x[0] -
另外,您不必将
input转换为字符串。在python中,输入默认是一个字符串。 -
请添加 time_limit 值以解决超时问题,以便会员可以提供帮助,检查输入只需执行
typed_word == x[0]
标签: python list time list-comparison