2

I'm very new to Python and having a problem with a program I'm doing for a class. main() and create_file work, but when it gets to read_file, the interpreter just sits there. The program is running but nothing is happening.

The answer is probably something very simple, but I just can't see it. Thanks in advance for any help.

I'm using IDLE (Python and IDLE v. 3.5.2)

Here's the code:

import random

FILENAME = "randomNumbers.txt"

def create_file(userNum):

    #Create and open the randomNumbers.txt file
    randomOutput = open(FILENAME, 'w')

    #Generate random numbers and write them to the file
    for num in range(userNum):
        num = random.randint(1, 500)
        randomOutput.write(str(num) + '\n')

    #Confirm data written
    print("Data written to file.")

    #Close the file
    randomOutput.close()

def read_file():

    #Open the random number file
    randomInput = open(FILENAME, 'r')

    #Declare variables
    entry = randomInput.readline()
    count = 0
    total = 0

    #Check for eof, read in data, and add it
    while entry != '':
        num = int(entry)
        total += num
        count += 1

    #Print the total and the number of random numbers
    print("The total is:", total)
    print("The number of random numbers generated and added is:", count)

    #Close the file
    randomInput.close()

def main():

    #Get user data
    numGenerate = int(input("Enter the number of random numbers to generate: "))

    #Call create_file function
    create_file(numGenerate)

    #Call read_file function
    read_file()

main()
5
  • 1
    while entry != '': You never change entry inside the loop, so it will keep looping forever. Commented Oct 6, 2016 at 22:13
  • Are you sure it's actually running? You defined some functions, but did you ever call them? Commented Oct 6, 2016 at 22:14
  • @Gator_Python Look at the last line: main() Commented Oct 6, 2016 at 22:14
  • Ah, thanks @Barmar I didn't scroll down. Commented Oct 6, 2016 at 22:15
  • @Barmar You're right - I forgot to put entry = randomInput.readline() in the while loop - thank you! I feel a bit stupid, lol. Commented Oct 6, 2016 at 23:35

1 Answer 1

6

You have an infinite while loop in the function, since entry never changes during the loop.

The Pythonic way to process all the lines in a file is like this:

for entry in randomInput:
    num = int(entry)
    total += num
    count += 1
Sign up to request clarification or add additional context in comments.

3 Comments

You can make it even more pythonic by wrapping using a context manager.
Thank you! For some reason I thought the while loop would stop when it reached the end of the file.
I realize I forgot to put entry = randomInput.readline() inside the while loop. Ugh, I feel stupid.

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.