Newbie Loop Question (Python)

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

Re: Newbie Loop Question (Python)

Post by insomaniacal on Thu Jul 09, 2009 9:30 pm
([msg=26577]see Re: Newbie Loop Question (Python)[/msg])

I think what he wants is for string "x" to be printed over and over while the computer waits for input from the user. So something like this...

Code: Select all
**Program Starts**
**Program starts waiting for user input**
**Program printing "x" until user input**
x
x
x
x
x
x
x
**user presses a key/types something**
y
Where "x" is the string he wants to be printing, and y is whatever happens after a user hits a key.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by tgoe on Fri Jul 10, 2009 1:23 am
([msg=26584]see Re: Newbie Loop Question (Python)[/msg])

Python 2x/3x input methods only block and won't work for this. I think you can make it work with the python curses bindings though. In particular the getch(), nodelay() and halfdelay() methods.
User avatar
tgoe
Contributor
Contributor
 
Posts: 633
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Spectre557 on Fri Jul 10, 2009 10:53 am
([msg=26611]see Re: Newbie Loop Question (Python)[/msg])

insomaniacal wrote:I think what he wants is for string "x" to be printed over and over while the computer waits for input from the user. So something like this...

Code: Select all
**Program Starts**
**Program starts waiting for user input**
**Program printing "x" until user input**
x
x
x
x
x
x
x
**user presses a key/types something**
y
Where "x" is the string he wants to be printing, and y is whatever happens after a user hits a key.

That's pretty much exactly what I was looking for, thanks for clarifying.

tgoe wrote:Python 2x/3x input methods only block and won't work for this. I think you can make it work with the python curses bindings though. In particular the getch(), nodelay() and halfdelay() methods.

I have no previous experience with those, so would you mind explaining them a little bit please?
Current obsession: Minecraft
User avatar
Spectre557
Poster
Poster
 
Posts: 215
Joined: Wed Apr 29, 2009 4:04 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by tgoe on Fri Jul 10, 2009 4:03 pm
([msg=26614]see Re: Newbie Loop Question (Python)[/msg])

It's ugly but here goes... You basically have to treat the absence of input as an error, catch the error and then ignore it :)

Here's an animated prompt just to show that you can modify the screen while waiting for input:
Code: Select all
#!/usr/bin/env python

import curses

screen = curses.initscr()
inp = ""
word = "quit"

for i in range(10000):
    # modify the screen
    screen.clear()
    screen.addstr("Type 'quit' to quit: (" + inp + ") " + "!" * (i % 10) +"\n")
    screen.refresh()

    # collect input if present or throw an error
    try:
        curses.halfdelay(1)
        inp += chr(screen.getch())
    except:
        pass

    # use the input in some way
    if inp == word:
        break
    if len(inp) > len(word):
        inp = ""

curses.endwin()


Continously scrolling text would take a bit more work but the idea is the same.
User avatar
tgoe
Contributor
Contributor
 
Posts: 633
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Spectre557 on Sun Jul 12, 2009 6:39 pm
([msg=26702]see Re: Newbie Loop Question (Python)[/msg])

tgoe wrote:It's ugly but here goes... You basically have to treat the absence of input as an error, catch the error and then ignore it :)

Here's an animated prompt just to show that you can modify the screen while waiting for input:
Code: Select all
#!/usr/bin/env python

import curses

screen = curses.initscr()
inp = ""
word = "quit"

for i in range(10000):
    # modify the screen
    screen.clear()
    screen.addstr("Type 'quit' to quit: (" + inp + ") " + "!" * (i % 10) +"\n")
    screen.refresh()

    # collect input if present or throw an error
    try:
        curses.halfdelay(1)
        inp += chr(screen.getch())
    except:
        pass

    # use the input in some way
    if inp == word:
        break
    if len(inp) > len(word):
        inp = ""

curses.endwin()


Continously scrolling text would take a bit more work but the idea is the same.

I think I get it now, but I might have to read through that a few more times :)

I'm awful at reading anyone else's code.

Thanks!
Current obsession: Minecraft
User avatar
Spectre557
Poster
Poster
 
Posts: 215
Joined: Wed Apr 29, 2009 4:04 am
Blog: View Blog (0)


Re: Newbie Loop Question (Python)

Post by Defience on Tue Jul 14, 2009 2:55 pm
([msg=26824]see Re: Newbie Loop Question (Python)[/msg])

Here are a couple of things you can also play around with and maybe incorporate into what you're looking for:

Code: Select all

import time
while True:
    response = raw_input("Are you an elite hacker?  ")
    if response.lower() == "yes":
         print "Can you hack a myspace for me then?!"
         break
    elif response.lower() == "no":
        print "Poser! Do you know anyone that is?"
        break
    else:
        while True:
            print "Please enter a yes or no or I will keep asking the same question!"
            time.sleep(2)


Use ctrl + c to get that ^^ to stop running. Of course you could change the time or remove it all together.

Code: Select all

import time
response=raw_input("Are you ready?\n")
while (response.lower() != "yes"):
  if (response.lower() != "no"):
    time.sleep(2)
    response=raw_input("\nAre you ready?\n")
raw_input("let's get started!")

User avatar
Defience
Addict
Addict
 
Posts: 1275
Joined: Thu Jun 12, 2008 3:16 pm
Blog: View Blog (0)


Previous

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests