【问题标题】:If statement by comparing player input and a list and timer not working as intended如果通过比较玩家输入和列表和计时器的语句未按预期工作
【发布时间】: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)的原因是我可以准确地验证从列表中提取的内容。请问我真的需要一些帮助!!!

输出图像:

https://ibb.co/n0KY70Y

【问题讨论】:

  • 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


【解决方案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

【讨论】:

  • 比较不是问题。问题是单词列表包含一个无用的额外列表级别。它应该只是一个字符串列表。这个答案只会延续一个设计错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-02
  • 2020-03-04
相关资源
最近更新 更多