gpt4 book ai didi

python - 无法获取要在 python 应用程序中显示的结果

转载 作者:行者123 更新时间:2023-12-01 05:09:31 25 4
gpt4 key购买 nike

我想做的是编写一个程序,读取文本文件中的数字,显示数字,然后显示文件中所有数字的总和并列出​​文件中的数字

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

我无法让它显示数字或给我总数。我需要纠正什么才能让它执行我想要的操作?

最佳答案

尝试

    print('There were ' + str(count) + ' records')

关于python - 无法获取要在 python 应用程序中显示的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24473020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com