RPython While Loop Skips First input

This is the place for queries that don't fit in any of the other categories.

RPython While Loop Skips First input

Postby kirbyfan64sos » Tue Aug 27, 2013 6:49 pm

I have this code:

Code: Select all
import os, rpython.rlib.rrandom as rrandom

def input(prompt, c=1):
    os.write(1, prompt)
    return os.read(0, c)

def pause(prompt):
    os.write(1, prompt)
    os.read(0, 1)

def write(text):
    os.write(1, text)

def random(rand):
    r = -1
    while True:
        r = int(rand.random() * 10)
        if (r >= 1) and (r <= 6):
            break
    return r

def roll(dice):
    rand = rrandom.Random()
    while True:
        for i in range(0, dice - 1):
            write(str(random(rand)) + ' ')
        write(str(random(rand)) + '\n')
        res = pause('Press Enter to roll again or type \'q\' and press Enter to exit...')
        if res == 'q':
            break
   write('\n')

def main(argv):
    write('Dice Roll Beta 1\n')
    dice = int(input('Number of dice to roll: '))
    write('\n')
    roll(dice)
    return 0

def target(driver, args):
    return main, None


It compiles fine. Running it, however, causes this:
Code: Select all
Dice Roll Beta 1
Number of dice to roll: 4

5 6 5 4
Press Enter to roll again or type 'q' and press Enter to exit...6 4 3 5
Press Enter to roll again or type 'q' and press Enter to exit...
5 4 1 6
Press Enter to roll again or type 'q' and press Enter to exit...

At the first call to
Code: Select all
pause
, it prints the prompt, but it doesn't actually pause; it just loops again. Then, the second loop and up, it pauses properly. I don't have a clue what's wrong.
This program is bug-free. If you see a bug, it's actually a feature.
kirbyfan64sos
 
Posts: 7
Joined: Sun Aug 25, 2013 10:45 pm

Re: RPython While Loop Skips First input

Postby micseydel » Wed Aug 28, 2013 7:30 am

Does this code enlighten you as to what's going on?
Code: Select all
>>> def test():
...     print ''.join(os.read(0, 1) for _ in xrange(4))
...
>>> test()
123456789
1234
>>> 56789
56789
>>>

Note that after test(), I only pressed enter a single time.

I recommend highly against the input and output method you're using here, and even more so against overwriting the built-in input() function. That was very confusing at first as I tried to understand what was going on in your code.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1507
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: RPython While Loop Skips First input

Postby Mekire » Wed Aug 28, 2013 9:10 am

micseydel wrote:I recommend highly against the input and output method you're using here, and even more so against overwriting the built-in input() function. That was very confusing at first as I tried to understand what was going on in your code.

I don't know anything about this but I was trying to read up on RPython to see why he was doing this and it sounds like he may have to do it this way.

http://www.hashbang.co.nz/blog/2013/5/23/rpython_is_not_a_language_an_introduction_to_the_rpython_language

Amongst that thread is specifically the line:
HINT 3: Redefine built-in functions that you want to use

I really don't know,
-Mek
User avatar
Mekire
 
Posts: 1143
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: RPython While Loop Skips First input

Postby kirbyfan64sos » Wed Aug 28, 2013 3:03 pm

RPython has to input or raw_input. Attempting to use any built-in functions pretty much gives a compile-time error.

I had used join originally, but I changed it to make sure it wasn't a glitch in the RPython implementation of join and forgot to change it back.
This program is bug-free. If you see a bug, it's actually a feature.
kirbyfan64sos
 
Posts: 7
Joined: Sun Aug 25, 2013 10:45 pm

Re: RPython While Loop Skips First input

Postby kirbyfan64sos » Wed Aug 28, 2013 7:17 pm

Solved! It was that the newline was adding a character to the buffer. The buffer only takes one character, so the newline was being added to the next read. The code:

Code: Select all
import os, rpython.rlib.rrandom as rrandom

def input(prompt, c=31366):
    os.write(1, prompt)
    res = os.read(0, c)
    return res.rstrip('\n')

def write(text):
    os.write(1, text)

def random(rand):
    r = -1
    while (r < 1) or (r > 6):
        r = int(rand.random() * 10)
    return r

def roll(dice):
    rand = rrandom.Random()
    res = ''
    while res != 'q':
        for i in range(0, dice - 1):
            write(str(random(rand)) + ' ')
        write(str(random(rand)) + '\n')
        res = input('Press Enter to roll again or type \'q\' and press Enter to exit...', 2)

def main(argv):
    write('Dice Roll Beta 1\n')
    dice = int(input('Number of dice to roll: ', 10))
    roll(dice)
    return 0

def target(driver, args):
    return main, None
This program is bug-free. If you see a bug, it's actually a feature.
kirbyfan64sos
 
Posts: 7
Joined: Sun Aug 25, 2013 10:45 pm


Return to General Coding Help

Who is online

Users browsing this forum: marcin_p and 5 guests