I'd like a code in which a raw_input function will end and move to a print function after 35 seconds.
So something like:

var = raw_input() # put time limit of 35 secs here
print "Time's up!" # prints this only if time runs out in the raw_input

Sadly, an easy alternative to raw_input that does not block (that is portable) is not too easy to find.

I tried to create a version myself, but apparently raw_input even blocks other threads in the program from printing to the console. It's pretty strange.

However, if this is just for yourself or only for one platform, take a look at these postings I found around the web:

Windows
Linux

Good luck.

It doesn't have to be raw_input, as long as it's a prompting function that can be connected to a variable.

You can use ' threading ' module.

import threading
import time
def msg():
    print "time's up"
    time.sleep(2)#just enough to show the above msg    
    exit()
t=threading.Timer(5.0,msg)
t.start()

But you have to figure out how to use it with Raw_input. Don't hesitate to ask.Go on.

So, uh, explain that to thy newb, please (I mean myself).

if you did't understand the code .once again

import threading
import time
def msg():
    print "time's up"
    time.sleep(2)#just enough to show the above msg    
    exit()
t=threading.Timer(5.0,msg)
t.start()

Since I'm not a guru myself(As u can see I'm a Newbie poster)so I'm looking for it myself.
I'll inform u if I got anything useful.

A simple example using Tkinter's after feature. You would probably want after to call a clean-up function to check for partial entries, etc., and then call quit().

import Tkinter

class TestClass():

   def __init__(self):
      
      self.top = Tkinter.Tk()
      self.top.title("Test of Timing")
      self.top.geometry("200x150+10+10")

      label_1 = Tkinter.Label(self.top, text="Timer Test ").pack(side="left" )
      entry_1 = Tkinter.Entry(self.top, width=10).pack(side="left")

      self.top.after(5000, self.top.quit)     ## 5 seconds
      self.top.mainloop()
         
if __name__ == '__main__':
   CT=TestClass()

I don't want images. And I want to be able to prompt.

For windows you can use the msvcrt module:

import msvcrt
import sys

time_is_up = False  #use threads to modify this
userinput = ""

while True:
    if msvcrt.kbhit():
        ch = msvcrt.getch()
        if ch != "\r" and not time_is_up:
            userinput += ch
            sys.stdout.write(ch)
        else:
            break

EDIT: As woooee posted, Tkinter's after function would probably be optimal if you plan on creating a GUI for your script. If not, then the following is probably what you are looking for:

Well apparently I was slightly wrong in my last post. Although raw_input() does block, it is indeed possible to get around with threads. In the piece of code I created myself, (similar to diwakar's) I tested it within an IDE and it did not work. However, when I ran it under normal conditions, it did.

Here's the code if you would like:

#!/usr/bin/python

import threading
from time import sleep

got_input = False

def printStuff():
    print
    print "Sorry, you didn't enter anything."
    print "Have a nice day."

class TimeThread(threading.Thread):
    def __init__(self, max_):
        threading.Thread.__init__(self)
        self.max_ = max_
        
    def run(self):
        sleep(self.max_)
        if (got_input == False): # the time is up: if the user didn't enter anything, print stuff.
            printStuff()
        
time_thread = TimeThread(3)
time_thread.start()
print "Enter your name: "
name = raw_input("> ")
got_input = True # if this code is executed, it means the user did input something
print "Welcome,", name

Thank you! Before I close the thread, how does SoulMazer's script work? I'd like to know for future references.

It doesn't have to be raw_input, as long as it's a prompting function that can be connected to a variable.

I don't want images. And I want to be able to prompt.

Make up your mind. Those of us who volunteer have a limited amount of time to spend here and do not wish to waste it generating code that will not be used.

Make up your mind. Those of us who volunteer have a limited amount of time to spend here and do not wish to waste it generating code that will not be used.

Uh.. it's solved.

Another problem; SoulMazer's code doesn't work. Yes, it gives you a time, but it doesn't end the action. Sorry if I'm taking up time, but I'm desperate to finish this game of mine. :(

Another problem; SoulMazer's code doesn't work. Yes, it gives you a time, but it doesn't end the action. Sorry if I'm taking up time, but I'm desperate to finish this game of mine. :(

Are you on windows or linux?
Or do you want to target both?

Another problem; SoulMazer's code doesn't work. Yes, it gives you a time, but it doesn't end the action. Sorry if I'm taking up time, but I'm desperate to finish this game of mine. :(

What do you mean? The code prompts you for a string, and if you do not enter one within X seconds, it prints some messages. Yes, I do have to admit that it does not cancel the original raw_input call, so the user can still input text, but that's why I originally said it was not truly possible.

If you look back to the links I provided in my first post, it provides alternatives to raw_input for both Windows in Linux without a GUI.

If you want more specific help, please be more specific with your problem, as woooee stated.

What do you mean? The code prompts you for a string, and if you do not enter one within X seconds, it prints some messages. Yes, I do have to admit that it does not cancel the original raw_input call, so the user can still input text, but that's why I originally said it was not truly possible.

If you look back to the links I provided in my first post, it provides alternatives to raw_input for both Windows in Linux without a GUI.

If you want more specific help, please be more specific with your problem, as woooee stated.

Sorry.

So here it is.

I'm making a game right now, and I am making a challenge mode. So I'd like the raw_input to stop functioning after a certain period of time.

My guess is to make the raw_input a function and break it after time passes. That's my guess, not sure if it will work.

Sorry.

So here it is.

I'm making a game right now, and I am making a challenge mode. So I'd like the raw_input to stop functioning after a certain period of time.

My guess is to make the raw_input a function and break it after time passes. That's my guess, not sure if it will work.

No, that won't work unless if you write your own raw_input function (e.g. on Windows this would be using msvcrt.getch, etc).
Do you want a cross-platform solution? Or do you want only Windows/Linux?

No, that won't work unless if you write your own raw_input function (e.g. on Windows this would be using msvcrt.getch, etc).
Do you want a cross-platform solution? Or do you want only Windows/Linux?

Cross-Platform, if possible. I may have to work with this on a Mac.

Woohoo! Finally done. Thank you for everyone's support!

I am also a newby to Python, my issue with raw_input is not that simple.

I'm currently using PyUIQ so msvcrt is out the window. I'm looking for an alternative to raw_input since the builtin one hangs the console or raises an error, please help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.