0

To start things off, this is my assignment(used on Python 3.4):

enter image description here

enter image description here

enter image description here

Here is my code. I think I have a pretty good hang of it:

# import random module
import random

# When doread() returns the maximum integer
# It is printed to the screen.
def main():
# call dowrite() and doread().
    dowrite()
    doread()

def dowrite():
    print ('Write will create 11 random numbers')
    print ('The number contained in mynumbers.txt are:')
    print('')
# generate random int num that is 5 < x < 13 and print num out
    random_int = random.randint(6, 12)
    print (random_int)
# use loop to generate num rand ints in range from 10-20 and write ints to mynumbers.txt.
# duplicates acceptable, w/ each int written to one line
myfile = open('mynumbers.txt', 'w')
for count in range(20):
        number = random.randint(10, 20)
        myfile.write(str(number) + '\n')

def doread():
# open the mynumbers.txt and read all numbers from file using for loop
    myfile = open('mynumbers.txt', 'r')
#    line = myfile.readline()
# Read each line in the file and display
for line in myfile:
    print(line)
# Print each number as it is read and find the largest number.
    with open('mynumbers.txt', 'r') as myfile:
        largest = max(map(int, myfile))
        print('The largest number in the file is: ',largest)

# Close the file and return largest number
myfile.close()

Again, as I said, I think I got the hang of it. Feel free to correct me otherwise, but my main problem is what happens when I press F5:

enter image description here

If someone could help me with this error, I could probably take it from here.

1
  • I think your identation is wrong, and you are not calling your main function. Commented Nov 7, 2015 at 2:25

2 Answers 2

2

I'm sure that you're looking for something like this...

import random

def main():
    dowrite()
    doread()

def dowrite():
    print ('Write will create 11 random numbers')
    print ('The number contained in mynumbers.txt are:')
    print()

    # only need open the file once here, and we don't need print the numbers now
    with open('mynumbers.txt', 'w') as myfile:
        for count in range(20):
            number = random.randint(10, 20)
            myfile.write(str(number) + '\n')

def doread():
    # first we print all of the numbers in that file
    with open('mynumbers.txt', 'r') as myfile:
        for line in myfile:
            print(line.strip())
        print()

        # then, use seek() here to check the largest number
        myfile.seek(0)
        largest = max(map(int, myfile))
        print('The largest number in the file is: ', largest)

main() # don't forget call main function

Demo:

Write will create 11 random numbers
The number contained in mynumbers.txt are:

20
16
12
11
18
10
16
17
13
11
10
14
14
16
16
17
19
17
11
11

The largest number in the file is:  20
Sign up to request clarification or add additional context in comments.

1 Comment

When I ran the code, it said 'myfile' was not defined, but I fixed that and defined 'myfile' as local variables in doread() and dowrite().
1

The indentation here looks questionable:

def doread():
# open the mynumbers.txt and read all numbers from file using for loop
    myfile = open('mynumbers.txt', 'r')
#    line = myfile.readline()
# Read each line in the file and display
for line in myfile:
    print(line)
# Print each number as it is read and find the largest number.
    with open('mynumbers.txt', 'r') as myfile:
        largest = max(map(int, myfile))
        print('The largest number in the file is: ',largest)

Here you've defined this function:

def doread():
# open the mynumbers.txt and read all numbers from file using for loop
    myfile = open('mynumbers.txt', 'r')

and added this code at the top level of the program, when it looks like you meant to make it part of your doread function?

for line in myfile:
    print(line)
# Print each number as it is read and find the largest number.
    with open('mynumbers.txt', 'r') as myfile:
        largest = max(map(int, myfile))
        print('The largest number in the file is: ',largest)

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.