我要做的是编写一个程序,读取文本文件中的数字,显示数字,然后显示文件中所有数字的总和,并列出文件中的数字
'''
This program should total the random numbers you generated in the numbers.txt file and
then list them and give a total of the numbers
'''
import random
def main():
# open random number file
number_file = open('numbers.txt', 'r')
# initialize an accumulator
total = 0.0
# Initialize a variable to keep count of the numbers
count = 0
# Get the values of the number file
for line in number_file:
# convert a line to a float
rand_number = float(line)
# add 1 to the count variable
count += 1
# Add the time to total
total += rand_number
# Close the file
number_file.close()
print('The total of the numbers is ', total)
print('There were' count + ' records')
# call main function
main()
我无法显示数字,也不能给我总数。我需要纠正什么才能让它做我想做的事情?
答案 0 :(得分:2)
试
print('There were ' + str(count) + ' records')
答案 1 :(得分:0)
如果你有2.7或更早版本的python,你可以使用:
print 'There were', count, 'records'