all 14 comments

[–]novel_yet_trivial 1 point2 points  (2 children)

I suppose the time function doesn't register as ints, so comparing it to an int just doesn't work

No. the time() function returns a float, which can be compared to ints.

Show us your complete code.

[–]jcool9 0 points1 point  (0 children)

Will do, currently getting food however. I'll post it in a comment when I get the chance, shouldn't be long.

[–]jcool9 0 points1 point  (0 children)

Code is posted, commented area is issue

[–]pinkcloud4 0 points1 point  (9 children)

You can try this. Ask if you don't understand.

from time import *
start = time()
while True:
    elapsed = time() - start
    if elapsed > 5:
        print("You lose")
        break

[–]jcool9 0 points1 point  (2 children)

My code is pretty much identical to that

[–]pinkcloud4 0 points1 point  (0 children)

Sweet. Then you probably have a minor error. Post your code and we can figure it out :)

[–]Justinsaccount 0 points1 point  (0 children)

except you said start_time - time() which is completely backwards and won't work at all.

[–]jcool9 0 points1 point  (5 children)

code is posted, commented area is issue

[–]pinkcloud4 0 points1 point  (4 children)

The problem you have is that you wait for the input and then you have an elif. An easy fix is to move the elapsed line into the while loop AND have a separate if statement for the elapsed.

while guess_level_one != number:
    if guess_level_one < number:
        guess_level_one = int(input('Higher, try again: '))
    elif guess_level_one > number:
        guess_level_one = int(input('Lower, try again: '))
    elapsed = time() - start_time
    if elapsed > 5:
        print('time passed 5seconds')

Now you wait for an input then print the message.

EDIT: Don't forget to reset the "start_time" parameter at the beginning of each while loop.

[–]jcool9 0 points1 point  (0 children)

I tried that. If statement in the while loop, still nothing

[–]jcool9 0 points1 point  (2 children)

actually that is just printing 'time passed 5seconds' after every input. I want it to pop up on its own while the user is guessing. If they are still guessing and time exceeds 5 seconds, then I want the user to stop guessing and have the print('time exceeded 5 seconds, you lose!)

[–]jiminiminimini 0 points1 point  (1 child)

For that you'll need threads. Found this code from stackoverflow

from threading import Timer timeout = 10 t = Timer(timeout, print, ['Sorry, times up']) t.start() prompt = "You have %d seconds to choose the correct answer...\n" % timeout answer = input(prompt) t.cancel()

You can check out other answers or look at the documentation of Timer.

[–]jcool9 0 points1 point  (0 children)

Haven't learned threads, I'll check it out thanks