Search code examples
pythonlisttimelist-comparison

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

Hi everyone, I just started out on a new project that tests your typing speed but ran into some issue i just can't seem to solve. First, the timer seems to go over the 10 second limit as shown below in the image url. Second, I can't seem to make to program verify the word the player inputs. It always outputs ~wrong~ as shown below in the image url. Each time I print a word from a list, it always prints with the [''] which makes the aesthetics of the game look unpleasant. So, I first thought the problem was due to me removing the [''] by using print(*x, "\n", sep = '') so, I tried alternative inputs to see whether if I can get typed_word == x. However, it seems futile as shown in the image url. Also the reason why i asked the program to print(x) was so I could verify exactly what was being pulled from the list. Please I really need some help!!!

Image of the outputs:

https://ibb.co/n0KY70Y


Solution

  • The two lines that need code change to check for the correct word are :

    #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]:
    

    Here's the full code for your reference. The only two lines I changed are the ones above and setting the values for start_time and time_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
    

    The output is:

    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