从Python中的代码获取不正确的输出
对于这个函数,我应该读取一个包含12个随机数的文件。然后我应该每行输出1个数字,最后程序应该分离偶数和赔率,然后将它们相加并显示它们的总数。这里的问题是,尽管我可以很好地打印数字,但最终total函数会弄乱并给出错误的总数从Python中的代码获取不正确的输出,python,python-3.x,Python,Python 3.x,对于这个函数,我应该读取一个包含12个随机数的文件。然后我应该每行输出1个数字,最后程序应该分离偶数和赔率,然后将它们相加并显示它们的总数。这里的问题是,尽管我可以很好地打印数字,但最终total函数会弄乱并给出错误的总数 def main(): infile = open('numbers.txt','r') line1 = infile.readline() line2 = infile.readline() line3 = infile.readline
def main():
infile = open('numbers.txt','r')
line1 = infile.readline()
line2 = infile.readline()
line3 = infile.readline()
line4 = infile.readline()
line5 = infile.readline()
line6 = infile.readline()
line7 = infile.readline()
line8 = infile.readline()
line9 = infile.readline()
line10 = infile.readline()
line1 = line1.rstrip('\n')
line2 = line2.rstrip('\n')
line3 = line3.rstrip('\n')
line4 = line4.rstrip('\n')
line5 = line5.rstrip('\n')
line6 = line6.rstrip('\n')
line7 = line7.rstrip('\n')
line8 = line8.rstrip('\n')
line9 = line9.rstrip('\n')
line10 = line10.rstrip('\n')
print(line1)
print(line2)
print(line3)
print(line4)
print(line5)
print(line6)
print(line7)
print(line8)
print(line9)
print(line10)
line = infile.readline()
total = 0
evenTotal = 0
oddTotal = 0
while line != '':
total += int(line)
if int(line) % 2 == 0:
evenTotal += int(line)
else:
oddTotal += int(line)
line = infile.readline()
print("=======================================")
print('The total for the even numbers is', evenTotal)
print("=======================================")
print('The total for the odd numbers is', oddTotal)
infile.close()
main()
这是文件中的内容
47
64
67
40
91
98
82
2
42
84
48
96
不知何故,两个总数我只得到了0
有人能帮上忙吗?由
open
返回的file对象维护一个指针,指向您当前在文件中的位置。每次调用infle.readline()
时,它都会将指针前进到下一行
因为在测试代码的过程中,您需要提前阅读(并打印)每一行代码,当您进入后面的代码时,您的文件已经到达了行值的末尾,并且不会神奇地返回到文件的开头
您可以重新打开文件,或者更简单地使用返回指向文件开头的文件指针。通过
open
返回的文件对象保留指向您当前在文件中位置的指针。每次调用infle.readline()
时,它都会将指针前进到下一行
因为在测试代码的过程中,您需要提前阅读(并打印)每一行代码,当您进入后面的代码时,您的文件已经到达了行值的末尾,并且不会神奇地返回到文件的开头
您可以重新打开文件,或者更简单地使用返回指向文件开头的文件指针。您应该学会使用列表理解或循环
infile = open('numbers.txt','r')
numbers = [int(line) for line in infile]
evens = [num for num in numbers if num % 2 == 0]
odds = [num for num in numbers if num %2 == 1]
您应该能够使用您拥有的代码完成其余部分,但是请注意,
sum([1,2,3,4,5])
返回15
你应该学会使用列表理解或循环
infile = open('numbers.txt','r')
numbers = [int(line) for line in infile]
evens = [num for num in numbers if num % 2 == 0]
odds = [num for num in numbers if num %2 == 1]
您应该能够使用您拥有的代码完成其余部分,但是请注意,
sum([1,2,3,4,5])
返回15
您的代码中有两个错误
line1
,line2
,等等--您希望更通用,以便您的编程问题解决方案更通用.readline
方法,您对该方法有点滥用。关于文件,一个真正重要的事情是,文件对象(由打开
返回的内容)是可编辑的,也就是说,您可以在for
循环中使用它们(例如,下面的第5行),并且在每次迭代中必须处理的对象是一行文本。也就是说,您可以按如下方式组织代码
# initialize your accumulators
odds = 0 ; evens = 0
# read lines from your file simply iterating on the file object!
for line in open('numbers.txt'):
# the int function ignores the white space, no need to strip
n = int(line)
% now you can print n
print(n)
# with an if...else... we update the accumulators
if n%2: # n is odd
odds += n
else:
evens += n
# we have read a line, converted in an integer, printed it,
# we updated the sums, we have done everything, hence
# we dedent the code, meaning that we are out of the loop
total = evens+odds
print("The sum of all even numbers is", evens)
...
您的代码中有两个错误
line1
,line2
,等等--您希望更通用,以便您的编程问题解决方案更通用.readline
方法,您对该方法有点滥用。关于文件,一个真正重要的事情是,文件对象(由打开
返回的内容)是可编辑的,也就是说,您可以在for
循环中使用它们(例如,下面的第5行),并且在每次迭代中必须处理的对象是一行文本。也就是说,您可以按如下方式组织代码
# initialize your accumulators
odds = 0 ; evens = 0
# read lines from your file simply iterating on the file object!
for line in open('numbers.txt'):
# the int function ignores the white space, no need to strip
n = int(line)
% now you can print n
print(n)
# with an if...else... we update the accumulators
if n%2: # n is odd
odds += n
else:
evens += n
# we have read a line, converted in an integer, printed it,
# we updated the sums, we have done everything, hence
# we dedent the code, meaning that we are out of the loop
total = evens+odds
print("The sum of all even numbers is", evens)
...
。下面是问题中所述问题的一个解决方案,以展示如何做到这一点
对于这个函数,我应该读取一个包含12个随机数的文件。然后我应该每行输出1个数字,最后程序应该分离偶数和赔率,然后将它们相加并显示它们的总数
。下面是问题中所述问题的一个解决方案,以展示如何做到这一点
对于这个函数,我应该读取一个包含12个随机数的文件。然后我应该每行输出1个数字,最后程序应该分离偶数和赔率,然后将它们相加并显示它们的总数
哇,你需要学习循环(参见示例)。但你在现代家庭里很棒!我相信您在设置line1=…,line2=…,时已经阅读了文件中的所有行。。。意思线等于零。您可以阅读while循环中的行,去掉换行符并将数字添加到evenTotal/oddtotall。几天前有一个非常类似(意思几乎完全相同)的问题。在什么地方有什么课程吗?关于这个问题的答复。他们也会处理很多困扰你的小事情,并且很容易帮助你@ljetibo在美国,一个新的学期最近开始了:)不相关,因为你也在维护一个
total
你不需要evenTotal
和oddtall
,因为oddtall==total-evenTotal
:)哇,你需要学习循环(参见示例)。但你在现代家庭里很棒!我相信您在设置line1=…,line2=…,时已经阅读了文件中的所有行。。。意思线等于零。您可以读取while循环中的行,去掉换行符并将数字添加到evenTotal/oddtottalther