import random
a=int(input(‘Enter the no:’))
for x in range(a):
f=open(‘rn.txt’,“w+”)
number=random.random()
f.write(str(number)+ ‘\n’)
content=f.read()
print(content)
Above code generate only single value whereas this should be generating 10 random numbers. Please correct me where I am doing mistake and guide me accordingly.
Each time through the loop, you open the file, write one number
over-writing the number you had before, then close the file again.
This is probably the simplest way to fix it.
Open the file once, write the ten numbers, then close:
with open('rn.txt', 'w') as f:
for i in range(10):
number = random.random()
f.write(str(number) + '\n')
# Now read the file and see what's there:
with open('rn.txt', 'r') as f:
print(f.read())