2012-07-16 28 views
-1

請記住,我對python編碼還是很新的,因爲我只是進入python編碼類的第5章。牢記這一點,我試圖創建一個使用「while循環」的總和計算器來繼續,直到用戶輸入負數而不是正數。需要幫助修復/提煉python中的while循環求和計算器

如果我不是我的,我的問題的描述完全清楚,我將發佈確切的家庭作業問題就在這裏:

第5章,200頁,#8 數之和

寫一個程序一個while循環,要求用戶輸入一系列正數。用戶應輸入一個負數來表示該系列的結束。所有正數輸入後,程序應顯示其總和。

現在對於我至今寫的代碼:

def main(): 
    number = float(input('Please enter in a positive number: ')) 
    while number > 0: 
     positiveNumber() 
    while number < 0: 
     calculateTotal() 
     printTotal() 

def positiveNumber(): 
    number = float(input('If you are finished please enter a negative number.' + \ 'Otherwise, enter another positive number: ')) 
    while number > 0: 
     positiveNumber() 
    while number < 0: 
     calculateTotal() 
     printTotal() 

def calculateTotal(): 
    total = 0 + number 

def printTotal(): 
    print('The sum of your numbers is: ', total) 

main() 
  • 在第11行,我有「+ \」標誌那裏,因爲我想做的,以便有沒有進入空間看起來更乾淨的文字,但這似乎並不奏效。

我很抱歉,如果這個問題似乎「不好」,但我需要幫助,使清潔/工作總和計算器。如果有人可以看一下這段代碼並希望幫助我改進,我將不勝感激。謝謝!

最終編輯:

謝謝大家提供的信息豐富的答案!我學到了很多(對於「新手」=])。我用我的計算器Talon876的答案。再次感謝大家!

+1

那麼......哪部分壞了? – 2012-07-16 18:11:44

+0

nooby,它真的是「新手」:-) – Levon 2012-07-16 18:13:54

+0

輸入是一個壞主意......而是使用raw_input – 2012-07-16 18:14:41

回答

3

如果你想多條線路上要打印一個字符串,字符串中放了\n。 例如,

print "This is on the first line\nThis is on the second line" 

將輸出

This is on the first line 
This is on the second line 

它看起來像你混合使用遞歸while循環(調用來自自身內部的方法)。我建議使用一個while循環和一個輸入變量來檢查斷狀態(輸入爲< 0)

這將是這個樣子:

sum = 0 
number = float(input('Please enter in a positive number: ')) 
while number > 0: 
    sum = sum + number 
    number = float(input('If you are finished please enter a negative number.' + \ 'Otherwise, enter another positive number: ')) #fix this line using the information from the first part of the answer 

這將循環播放,直到用戶輸入一個負數,或0.如果要接受0作爲正數,請將while條件更改爲number > -1

1

如果不將其顯式聲明爲全局變量,則無法更新python函數中的全局變量。觀察到:

a = 1 
def foo(): 
    a = a + 6 #creates a new variable (a) that is confined to the "foo" namespace. 
      #Note that it still uses a from the global namespace on the Right hand side 
      #This is because python looks for a in the "foo" namespace first. When 
      #it isn't found there, it looks in the global namespace. However, python 
      #WON'T ASSIGN to something in the global namespace without being told 
      #to explicitly 
    print (a) 

foo() # 7 
print (a) # 1 

def foo(): 
    global a #Tell python that it is OK to assign to variable "a" in the global namespace. 
    a = a + 6 
    print (a) 

foo() # 7 
print (a) # 7 

但是,擁有這個巨大的力量有很大的責任。很多人會告訴你從不使用全局變量。在很多方面,它們是正確的,因爲幾乎所有可以用全局變量完成的任何事情都可以使用其他方法更加乾淨地完成。我寫這篇文章的目的不是爲了說服你使用全局變量,而是爲了幫助你理解代碼中的一個錯誤。

你可能想要嘗試的一件事是讓你的函數接受輸入數字作爲參數,並且總和到這一點,然後return新的總數。

祝你好運!

0

問題1.you還沒有宣佈你正在使用的功能作爲全球性的,要注意變化的變量他們正在取得

2.you不需要while循環,如果你是通過調用實現它!遞歸函數您需要檢查條件LIK「如果&其他」 這裏是一個容易實現的問題,while循環:

def main(): 
total=0 
number = float(input('Please enter in a positive number: ')) 
while(number>0): 
    total=total+number 
    number = float(input('Please enter in a positive number to continue or a negative no. to stop: ')) 
print('The sum of your numbers is: %d'% total) 
main() 
+1

你不會在這裏遞歸調用任何東西。 – mgilson 2012-07-16 18:44:35

0

我認爲你在尋找這樣的事情?但是我不知道你需要使用哪種風格約束。

number = float(input('Please enter in a positive number: ')) 
to_sum = [] 
while number > 0: 
    to_sum.append(number) 
    number = float(input('If you are finished please enter a negative number.\n' + 
         'Otherwise, enter another positive number: ')) 
print('The sume of your numbers is: ', sum(to_sum)) 

請注意,因爲您試圖分成多行的語句已經在()的範圍內,所以您不需要。你可以打破這條線。

該作業是否需要您使用如此多的瘋狂功能?另外,你使用的是哪個版本的Python?

0

你需要學習的一件事是如何將程序分解爲函數。一段代碼比一段代碼更好地處理一些問題,我認爲這是其中之一。

您需要計算一筆總和。您可以使用單個變量來處理該變量,並在用戶輸入時向其添加更多數字。你的代碼應該圍繞這個變量進行設計。如果你試圖將代碼分解成函數,你需要使用一個全局變量(不推薦!),或者你需要在函數中傳遞變量,或者你可以把變量放入一個對象中,然後該功能是對象上的「方法」功能。但最簡單的方法就是編寫一些使用變量的代碼,並將所有代碼作爲一個代碼塊(一個函數,或者甚至只是Python程序中的代碼)。

這裏是一個解決方案:

sum = 0.0 # initial value; we will add values to this 
print('Welcome to this program') 
while True: 
    s = input('User: enter data value or a negative number to stop') 
    x = float(s) 
    if x < 0: 
     break 
    sum += x # add this value to update the sum 
print('Here is your sum: {}'.format(sum)) 

所以,這裏是什麼好對上面的代碼。所有需要使用變量sum的地方都緊密結合在一起,我們可以看到它們。我們不需要聲明sum全局,因爲我們沒有多個函數試圖使用它。

看看這段代碼,問自己:如果我們把它分成多個函數,它會更簡單還是更清晰?如果不是,那就不要這樣做。

這裏唯一棘手的問題是我們使用while True:作爲循環。這是因爲我們想要做一些事情(得到輸入),然後根據結果決定是跳出循環還是繼續,然後根據決策做出其他事情(更新總和)。

這有可能改寫這個使用「標誌」變量,使循環while flag:,但我不認爲它是清潔:

sum = 0.0 # initial value; we will add values to this 
print('Welcome to this program') 
continue_loop = True 
while continue_loop: 
    s = input('User: enter data value or a negative number to stop') 
    x = float(s) 
    if x < 0: 
     continue_loop = False 
    else: 
     sum += x # add this value to update the sum 
print('Here is your sum: {}'.format(sum)) 

你是否認爲這是更清晰的有continue_loop標誌變量?一些教科書說你應該這樣寫你的代碼,因爲他們認爲使用break退出中間循環是一種罪過;他們認爲環路應該只從通常的地方退出(對於while環路來說,它是最高的)。

如果你真的想使用函數呢?那麼,你可以,但你仍然不應該使用全局變量。事實上,如果您正在編寫「功能性」解決方案,則根本不需要sum變量!

這是一個功能性解決方案。

def ask_and_sum(): 
    s = input('Hey dude enter a value or a negative to stop') 
    x = float(s) 
    if x < 0: 
     return 0 
    else: 
     return x + ask_and_sum() 

print('Welcome to this program') 
print('Your sum is: {}'.format(ask_and_sum())) 

而不是顯式循環,它使用「尾遞歸」,其中一個函數結束與另一個調用自己。在這種情況下,我個人更喜歡顯式循環。你怎麼看?

P.S.這個問題非常簡單,如果沒有給出完整的答案就很難討論它。我爲此道歉。但即使您只是複製代碼,請仔細閱讀並仔細考慮,並確保您瞭解它。