Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Python File I/O

Name: Anonymous 2010-10-04 10:21

I have a words.txt file that looks like:

the
of
and
to


And a python script that looks like:


import random
y = ""
z = 0

openfile = open('words.txt', 'r')
contents = openfile.read()
openfile.close()
print contents

for i in range (len(contents)):
    x = random.choice(contents)
    if x == """
""":
        y == """
"""
    else:
        y = ""
        print x
        z = z + 1
        while x != y:
            y = raw_input('>>>')
print z

I want the output to be the full words, but it outputs just letter like:


o
>>>o
o
>>>o
a
>>>a
n
>>>n
t
>>>t
e
>>>e
h
>>>h


Any help?

Name: Anonymous 2010-10-04 10:28

If your Python script looks like that, you need to configure your editor not to use a proportional font, and enable syntax highlighting. That's fucking illegible.

Name: Anonymous 2010-10-04 10:57

x = random.choice(contents)
Just a hunch.

Name: Anonymous 2010-10-04 11:02

Where you read, use readlines, not read, to read full lines. read reads a string, hence you're picking characters. readlines reads a list of strings, hence you'd be picking strings.

Name: Anonymous 2010-10-04 11:04

>>1
    if x == """
""":
IHBT :(

Name: Anonymous 2010-10-04 11:34

It's important, so I'lln't sage it.

I refuse to read any source code without proper [code]tags\[/code].

Name: Anonymous 2010-10-04 11:40


import random
y = ""
z = 0

openfile = open('words.txt', 'r')
contents = openfile.read()
openfile.close()
print contents

for i in range (len(contents)):
    x = random.choice(contents)
    if x == """
""":
        y == """
"""
    else:
        y = ""
        print x
        z = z + 1
        while x != y:
            y = raw_input('>>>')
print z


And then we did not have to suffer anymore

Name: Anonymous 2010-10-04 11:46

Why did you put an arbitrary limit on the number of guesses?

(import (only (srfi :13 strings) string-tokenize)
        (srfi :27 random-bits)
        (wak foof-loop))

(define (vector-pick vec)
  (let ((len (vector-length vec)))
    (vector-ref vec (random-integer len))))

(define read-line
  ;; Making standard-input-port return a binary port is a PITA
  ;; Damn you R6RS
  (let ((in (transcoded-port (standard-input-port) (native-transcoder))))
    (lambda ()
      (get-line in))))

(define (file->words file)
  (loop ((for line (in-file file get-line))
         (for words (appending (string-tokenize line))))
        => (list->vector words)))

(define contents (file->words "words.txt"))

(display (loop ((for z (up-from 0))
                (let answer (vector-pick contents))
                (let input (read-line))
                (until (string=? input answer )))
               => z
               (display answer)
               (newline)))

Name: Anonymous 2010-10-04 11:48

>>8
lisp faggot detected

Name: Anonymous 2010-10-04 11:49

Here comes the proportional calamity.

Name: Anonymous 2010-10-04 11:51

>>9
And what have you contributed to /prog/ recently, other than acting like a child?

Name: >>8 2010-10-04 12:07

The string-tokenize wasn't actually necessary but ho-hum

Name: Anonymous 2010-10-04 13:11

>>10,11
Fuck off, ``faggot''.

Name: Anonymous 2010-10-04 13:16

>>13,14,15
Fuck off, ``faggot''.

Name: 2010-10-04 13:31

Name: 2010-10-04 13:31

Fuck off, ``faggot''.

Name: Anonymous 2010-10-04 13:37

>>13
Here comes the imageboard paladin.

Name: Anonymous 2010-10-04 13:41

>>17
Fuck off, ``faggot''.

Name: Anonymous 2010-10-04 13:45

>>18
Here comes the imageboard paladin.

Name: Anonymous 2010-10-04 13:56

>>19
Fuck off, ``faggot''.

Name: Anonymous 2010-10-04 14:01

>>19
Here comes your mother

Name: Anonymous 2010-10-04 18:53

>>8
I don't know much of Lisp but I like what I know; however, can't you do better than that in Scheme? That code is clear but awfully long for such a trivial task. I've fixed >>1's program into something working (however strange) on the FIOC 3, and it's uncomfortably shorter and nimbler than your Scheme program.

import random
z = 0

contents = open('words.txt').read().split('\n')

for i in range(len(contents)):
    x = random.choice(contents)
    if x:
        print(x)
        z += 1
        while x != input('>>>'): pass

print(z)

Name: Anonymous 2010-10-04 20:09

>>22
I wouldn't say that my final loop is any longer than yours. I just had to write a few helpers to make up for a sparser standard library.

Name: >>23 2010-10-04 20:21

Well, I shaved off a few lines, but Scheme isn't very good for ``code golf''
(import (srfi :27 random-bits)
        (wak foof-loop))

(define (vector-pick vec)
  (let ((len (vector-length vec)))
    (vector-ref vec (random-integer len))))

(define stdin (transcoded-port (standard-input-port) (native-transcoder)))

(define (file->words file)
  (loop ((for line (in-file file get-line))
         (for words (listing line)))
        => (list->vector words)))

(define contents (file->words "words.txt"))

(display (loop ((for z (up-from 0))
                (let answer (vector-pick contents))
                (until (string=? (get-line stdin) answer)))
               => z
               (display answer)
               (newline)))

Name: Anonymous 2010-10-04 20:23

s/words/lines/g

Name: Anonymous 2010-10-04 20:55

>>24
Actually, I'm an idiot. The stdin shenanigans was unnecessary as, at startup time, (current-input-port) would be the textual version of (standard-input-port)

Name: ​​​​​​​​​​ 2010-10-26 7:10

Name: Anonymous 2011-02-03 4:27

Name: Anonymous 2011-02-04 17:02


Don't change these.
Name: Email:
Entire Thread Thread List