我对编程很陌生,这是我的第一个 python 脚本作业。有人可以告诉我我做错了什么吗?

该程序是关于分别给出从 1 到给定的 n 项的奇数和偶数的总和。 输入


print()

print("Program to display sum of n terms of odd/even natural numbers!")

print()

num = int(input("Enter the number of natural numbers: "))

even_total = 0

odd_total = 0

i = 1

while i == num:

    if(i % 2 == 0):

        even_total = even_total + i

        i += 1

    else:

        odd_total = odd_total + i

        i += 1

print()

print("The sum of even numbers from 1 to {0} = {1}".format(i, even_total))

print("The sum of odd numbers from 1 to {0} = {1}".format(i, odd_total))

输出:


Program to display sum of n terms of odd/even natural numbers!


Enter the number of natural numbers: 10


The sum of even numbers from 1 to 1 = 0

The sum of odd numbers from 1 to 1 = 0

>>> 


慕少森
浏览 150回答 5
5回答

德玛西亚99

print您的程序没有完成预期的循环交互,如果您在循环中放置一些额外的语句(例如 ),这会更加明显print(i)- 这是一种您可以在将来使用的简单调试技术。尽管实际上在您看到的输出中有一条线索,但它说的是from 1 to 1而不是类似from 1 to 10.发生的事情是第一次while i == num:测试时,它求值False(0 不等于 10),因此永远不会进入循环。==如果您将to更改<=为此处,那么这将主要解决问题(循环将达到并包括 10)。您可以进行的其他改进包括:在print最后的语句中,使用numinstead of&nbsp;i:print("The&nbsp;sum&nbsp;of&nbsp;even&nbsp;numbers&nbsp;from&nbsp;1&nbsp;to&nbsp;{0}&nbsp;=&nbsp;{1}".format(num,&nbsp;even_total))在循环内部,在和块i += 1中都完成了,因此您可以在 if...else... 之后执行一个无条件的。ifelsei += 1&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;even_total&nbsp;+=&nbsp;i&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;odd_total&nbsp;+=&nbsp;i &nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;+=&nbsp;1(我还建议+=在此处使用总计,就像您已经在使用 一样i。)您也可以使用for循环 using代替,然后您根本range不需要显式递增。i请注意, 的上限range必须比i上一次迭代的值大 1。for&nbsp;i&nbsp;in&nbsp;range(1,&nbsp;num&nbsp;+&nbsp;1):&nbsp; &nbsp;&nbsp;&nbsp;if&nbsp;i&nbsp;%&nbsp;2&nbsp;==&nbsp;0: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;even_total&nbsp;+=&nbsp;i&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;odd_total&nbsp;+=&nbsp;i

冉冉说

你正在使用这个while i==num: ,这意味着当loop第一次运行时它会采取num=1这意味着你while i==1是true和while loop休息。因此,它不能做总和。所以你可以使用while<=num这是你的代码print()print("Program to display sum of n terms of odd/even natural numbers!")print()num = int(input("Enter the number of natural numbers: "))even_total = 0odd_total = 0i = 1while i <= num:&nbsp; &nbsp; if(i % 2 == 0):&nbsp; &nbsp; &nbsp; &nbsp; even_total = even_total + i&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; odd_total = odd_total + i&nbsp; &nbsp; i += 1print()print("The sum of even numbers from 1 to {0} = {1}".format(num, even_total))print("The sum of odd numbers from 1 to {0} = {1}".format(num, odd_total))

料青山看我应如是

while loop你应该尝试用for loop 类似的替换for&nbsp;i&nbsp;in&nbsp;range(1,num+1):计算偶数和奇数和..或替换while i==num为i!=num+1(i 不等于 num) 或 (i<=num)

沧海一幻觉

循环的条件是问题。它应该是:while i<= num:

开满天机

您需要更换while i==num为while i<=numprint()print("Program to display sum of n terms of odd/even natural numbers!")print()num = int(input("Enter the number of natural numbers: "))even_total = 0odd_total = 0i = 1while i <= num:&nbsp; &nbsp; if(i % 2 == 0):&nbsp; &nbsp; &nbsp; &nbsp; even_total = even_total + i&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; odd_total = odd_total + i&nbsp; &nbsp; i += 1&nbsp; #a small optimization to reduce number of linesprint()print("The sum of even numbers from 1 to {0} = {1}".format(i-1, even_total))print("The sum of odd numbers from 1 to {0} = {1}".format(i-1, odd_total))因为,在退出 while 循环时你i将大于num,你应该i-1在循环后面的打印语句中打印。我输入4并得到以下输出:Program to display sum of n terms of odd/even natural numbers!Enter the number of natural numbers: 4The sum of even numbers from 1 to 5 = 6The sum of odd numbers from 1 to 5 = 4
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python