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

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 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)))

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