Results: Python: Anyone know how to write Random Number File Writer 

Voters
4 You may not vote on this poll

  • Yes
    2  50.00%
  • No
    1  25.00%
  • Maybe
    1  25.00%
  • What?
    0%
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Exclamation Random Number File Writer / PYTHON


    Hello I'm new to python and sort of getting the hang of it...
    sort of

    Heres what I have

    Instructions:
    #Write a program that writes a series of random numbers to a file.
    #Each random number should be in the range of 1 through 100.

    #The application should let the user specify how
    #many random numbers the file will hold.

    Heres what I have:

    import random

    afile = open("Random.txt", "w" )

    for line in afile:
    for i in range(input('How many random numbers?: ')):
    line = random.randint(0, 100)
    afile.write(line)
    print(line)

    afile.close()


    print("\nReading the file now." )
    afile = open("Random.txt", "r")

    print(afile.read())
    afile.close()


    Its a few things
    1. Its not writing the random numbers in the file the USER is setting.

    2. the file once opened cant close

    3. when the file is read nothing

    Please anyone....any ideas? I seem to always get stuck on execution with python.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    Code:
    import random
    
    afile = open("Random.txt", "w" )
    
    for i in range(input('How many random numbers?: ')):
        line = str(random.randint(0, 100))
        afile.write(line+str("\n"))
        print(line)
    
    afile.close()
    
    
    print("\nReading the file now." )
    afile = open("Random.txt", "r")
    
    print(afile.read())
    afile.close()
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Smile Wow!!!!!


    Holy crap....just wow...thankyou so much!!!!!!!!!!!

IMN logo majestic logo threadwatch logo seochat tools logo