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:
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:
Anonymous2010-10-04 10:57
x = random.choice(contents)
Just a hunch.
Name:
Anonymous2010-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.
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
(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)))
>>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
>>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)