0

Please help me guys... I do not know how to do this It should generate 15 random numbers, write the odd ones into a .txt file and then read it.

import random
f = open('text','w+')
numbers = []
for i in range(15):
    x = random.randint(0,25)
    if x%2 == 1:
        numbers.append(str(x))
        f.write(str(x) + ' ')
print(f.read())
3
  • 1
    You should open your file in open('text', 'rw+') mode. When you finish with writing odd numbers, you should jump to the start of the file using f..seek(0, 0) and then read form it. Commented Oct 18, 2019 at 19:04
  • @aminrd Please don't post answers as comments, as it bypasses important quality control factors such as downvotes, and also deprives you of those sweet sweet answer points. Commented Oct 18, 2019 at 19:07
  • Thanks, that f.seeked helped Commented Oct 18, 2019 at 19:09

4 Answers 4

1

How about this:

import random
f = open('text','w+')
numbers = []
#for i in range(15):
while len(numbers) < 15:
    x = random.randint(0,25)
    if x%2 == 1:
        numbers.append(str(x))
        f.write(str(x) + ' ')
f.close()

rf = open('text','r')
print(rf.read())
rf.close()

So one of the problems i spotted was that you range 15 but not necessarily every value will be odd.

I also close the file and re-opened it as read.

Sign up to request clarification or add additional context in comments.

Comments

1

Open file in "write" mode, write numbers, close it, and then open it in "read" mode. Hope that helps

import random
f = open('text.txt','w')
numbers = []
for i in range(15):
    x = random.randint(0,25)
    if x % 2 == 1:
        numbers.append(str(x))
        f.write(str(x) + ' ')
f.close()
f = open('text.txt', 'r')
print(f.read())
f.close()

Comments

0

One of the bugs was, if the random number was even, it didn't get added to the file - but the for i in range(15): continues to the next iteration - so you don't end up with 15 numbers.

import random
numbers = []
# With/open is the preferred technique to write. 
# Use a separate with open to read at the end. 
# Dont try to keep reading and writing from the same file, while it stays open
with open('text','w+') as f: 
    for i in range(15): # This will loop 15 times
        x = random.randint(0,25) # Set the random int first, so the while loop can evaluate
        while x%2 == 0: # if X is even, This will keep looping until it is odd
            x = random.randint(0,25)
        numbers.append(str(x)) # Dont know what this is for
        f.write(str(x) + ' ')

print("----------------")
with open('text','r') as f: 
    print(f.read())

Comments

0

You can open your file in open('text', 'rw+') mode. When you finish with writing odd numbers, you should jump to the start of the file using f.seek(0, 0) and then read form it. Like this:

import random
f = open('text','rw+')
numbers = []
for i in range(15):
    x = random.randint(0,25)
    if x%2 == 1:
        numbers.append(str(x))
        f.write(str(x) + ' ')
f.seek(0, 0)
print(f.read())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.