亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我對編程很陌生,這是我的第一個 python 腳本作業。有人可以告訴我我做錯了什么嗎?

我對編程很陌生,這是我的第一個 python 腳本作業。有人可以告訴我我做錯了什么嗎?

慕少森 2023-05-09 14:47:19
該程序是關于分別給出從 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 = 0odd_total = 0i = 1while i == num:    if(i % 2 == 0):        even_total = even_total + i        i += 1    else:        odd_total = odd_total + i        i += 1print()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: 10The sum of even numbers from 1 to 1 = 0The sum of odd numbers from 1 to 1 = 0>>> 
查看完整描述

5 回答

?
德瑪西亞99

TA貢獻1770條經驗 獲得超3個贊

print您的程序沒有完成預期的循環交互,如果您在循環中放置一些額外的語句(例如 ),這會更加明顯print(i)- 這是一種您可以在將來使用的簡單調試技術。盡管實際上在您看到的輸出中有一條線索,但它說的是from 1 to 1而不是類似from 1 to 10.

發生的事情是第一次while i == num:測試時,它求值False(0 不等于 10),因此永遠不會進入循環。==如果您將to更改<=為此處,那么這將主要解決問題(循環將達到并包括 10)。

您可以進行的其他改進包括:

  • print最后的語句中,使用numinstead of i

print("The sum of even numbers from 1 to {0} = {1}".format(num, even_total))
  • 在循環內部,在和塊i += 1中都完成了,因此您可以在 if...else... 之后執行一個無條件的。ifelsei += 1

    if i % 2 == 0:
        even_total += i 
           else:
        odd_total += i
    i += 1

(我還建議+=在此處使用總計,就像您已經在使用 一樣i。)

  • 您也可以使用for循環 using代替,然后您根本range不需要顯式遞增。i請注意, 的上限range必須比i上一次迭代的值大 1。

for i in range(1, num + 1): 
   if i % 2 == 0:
        even_total += i 
           else:
        odd_total += i


查看完整回答
反對 回復 2023-05-09
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

你正在使用這個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 = 0

odd_total = 0

i = 1

while i <= num:

    if(i % 2 == 0):

        even_total = even_total + i

    else:

        odd_total = odd_total + i

    i += 1

print()

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


查看完整回答
反對 回復 2023-05-09
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

while loop你應該嘗試用for loop 類似的替換

for i in range(1,num+1):

計算偶數和奇數和..

或替換while i==numi!=num+1(i 不等于 num) 或 (i<=num)


查看完整回答
反對 回復 2023-05-09
?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

循環的條件是問題。它應該是:

while i<= num:


查看完整回答
反對 回復 2023-05-09
?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

您需要更換while i==num為while i<=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 = 0

odd_total = 0

i = 1

while i <= num:

    if(i % 2 == 0):

        even_total = even_total + i

    else:

        odd_total = odd_total + i

    i += 1  #a small optimization to reduce number of lines

print()

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: 4


The sum of even numbers from 1 to 5 = 6

The sum of odd numbers from 1 to 5 = 4


查看完整回答
反對 回復 2023-05-09
  • 5 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號