Results 1 to 7 of 7

Thread: Newb Question, Python:How to await input while counting down with timert

  1. #1
    Join Date
    Apr 2012
    Beans
    17

    Newb Question, Python:How to await input while counting down with timert

    edit: There is a typo in the title. The last word should be "timer" not "timert"


    I know very little about programming. I've completed a few python tutorials several years ago, but I never stepped-up my knowledge. So I'm still a newb (actually I'm worse than a newb, but I know of no such term to designate that status). Frankly I have no aptitude for this, but I'm trying because programming knowledge is worthy of pursuit.

    My current project, which is still in the conceptual stage, is a very simple game that will present the user with various scenarios and about 4 possible decisions per scenario (depending on the scenario). To add some excitement, I want there to be a countdown that will end the game if the user runs out of time without making a valid decision.

    There are two things that I need to know in order for me to move forward and implement the above:

    1. I need to know the function (and, if applicable, the library I'll need to import in order to use it), that can cause a 1-second delay. I figure that I would use it in a loop to put a "halt" in the countdown. Of course it won't be exactly 1-second between numbers because of the execution time, but it's close enough for me.

    2. I need to know how to allow this countdown to go forward, while, at the same time having the program "listen" for the user's selection. I have no idea how to accomplish this. I know only of raw_input, and that brings the execution sequence to a screeching halt so it can await the user's input.

    Any suggestions?

    Thanks in advance.
    Last edited by LearnAlways; May 22nd, 2012 at 06:11 PM.

  2. #2
    Join Date
    Dec 2011
    Location
    The Netherlands
    Beans
    1,087
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Newb Question, Python:How to await input while counting down with timert

    I don't know python (i know 6+ other major languages though, excluding brain**** and other esoteric/shady/vague languages).
    But i'd say: MULTITHREADING
    "Everything that has a beginning, has an end." --an unexpectedly wise wallpaper

  3. #3
    Join Date
    Apr 2012
    Beans
    17

    Re: Newb Question, Python:How to await input while counting down with timert

    Thank you for the suggestion. But I was hoping there was a simpler way to accomplish this than multithreading. I thought multithreading was a fairly new technology, and I know that simultaneously counting down while simultaneously awaiting user input (or at least appearing to simultaneously do both) has been possible for decades (e.g. I remember playing super mario bros. way back in the 80s. That timer kept counting down while I was pushing buttons on the controller, trying to get through the level. I died a lot )

    If I try multithreading I'm afraid I'd be trying to "run" before I know how to properly "walk". Also, from what little I know of programming aesthetics, I think I'm supposed to seek out the most simple solution before trying something more complicated or advanced. Or maybe I'm supposed to use the best tool at my disposal?

    Is there any other approach that you or anyone else can recommend?

  4. #4
    Join Date
    Dec 2011
    Location
    The Netherlands
    Beans
    1,087
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Newb Question, Python:How to await input while counting down with timert

    Quote Originally Posted by LearnAlways View Post
    Thank you for the suggestion. But I was hoping there was a simpler way to accomplish this than multithreading. I thought multithreading was a fairly new technology, and I know that simultaneously counting down while simultaneously awaiting user input (or at least appearing to simultaneously do both) has been possible for decades (e.g. I remember playing super mario bros. way back in the 80s. That timer kept counting down while I was pushing buttons on the controller, trying to get through the level. I died a lot )

    If I try multithreading I'm afraid I'd be trying to "run" before I know how to properly "walk". Also, from what little I know of programming aesthetics, I think I'm supposed to seek out the most simple solution before trying something more complicated or advanced. Or maybe I'm supposed to use the best tool at my disposal?

    Is there any other approach that you or anyone else can recommend?
    Well, multithreading is what linux was designed to do.
    I've got a pc from 1992 in my room (the thing still works) and it has a i386SX (yes, the real 80368 series) that can do multithreading (if a processor has task-switching, it can do multithreading and even that 20yrs old thing has it).

    Just lookup a good tut on it, they're all over the web.
    "Everything that has a beginning, has an end." --an unexpectedly wise wallpaper

  5. #5
    Join Date
    Apr 2012
    Beans
    17

    Re: Newb Question, Python:How to await input while counting down with timert

    I didn't know that multithreading went back that far. And I didn't know linux was designed for it. That's pretty cool.

    I still don't know if multithreading is the right choice for me, as a novice programmer. But if it's the only way to accomplish this, then I'll have to use it. I'll wait and see what other people suggest, and do some additional research. At this point though, it does seem as if your multithreading suggestion is my best bet. Thanks again.

  6. #6
    Join Date
    May 2008
    Location
    UK
    Beans
    1,451
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Newb Question, Python:How to await input while counting down with timert

    Anothe way of doing this would be a main loop - which says :

    Code:
    sleep for 0.5 seconds 
    Look for input
    revise countdown
    Loop until time is up or input given
    If you want a more responsive program reduce the amount that you sleep.

    Multi threading is the preffered way to go though.
    Tony - Happy to try to help.
    Unless otherwise stated - all code posted by me is untested. Remember to Mark the Thread as Solved.
    Ubuntu user number # 24044 Projects : TimeWarp - on the fly Backups

  7. #7
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Newb Question, Python:How to await input while counting down with timert

    except threads in CPython are not truly concurrent.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •