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

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