从文件中读取数字时出错(Python)

时间:2015-11-07 02:15:14

标签: python file-io python-3.4

首先,这是我的任务(在Python 3.4上使用):

enter image description here

enter image description here

enter image description here

这是我的代码。我想我对它有很好的了解:

# 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()

同样,正如我所说,我认为我掌握了它。除此之外,请随意纠正我,但我的主要问题是当我按下F5时会发生什么:

enter image description here

如果有人可以帮我解决这个错误,我可以从这里开始。

2 个答案:

答案 0 :(得分:2)

我确定你正在寻找这样的东西......

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

演示:

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

答案 1 :(得分:1)

这里的缩进看起来有问题:

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)

这里你定义了这个功能:

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

并将此代码添加到程序的顶层,看起来您打算将其作为doread函数的一部分?

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)