2013-07-22 26 views
-3

我有一個任務。這是假設做到以下幾點: - >以整數輸入(比如100) - >添加數字,直到總和是一個單一的數字(1)來自多個數字的數字

我的計劃至今是:

goodvalue1=False 
goodvalue2=False 

while (goodvalue1==False): 
    try: 
     num=input("Please enter a number: ") 
    except ValueError: 
     print ("Wrong input. Try again.") 
    else: 
     goodvalue1=True 


if (goodvalue1==True): 
    ListOfDigits=list(map(int,str(num))) 
    sum=10 
    while(sum>9): 
     Sum=sum(ListOfDigits) 
     if (Sum>9): 
      ListOfDigits=list(map(int,str(Sum))) 
      Sum=sum(ListOfDigits) 
+2

那麼,這是什麼問題? –

+0

有些想法:你不需要檢查'if(something == True)',只是做'如果有事'。對於你的while循環,你可以做'while not goodvalue1'。另外,在if和while條件下,您不需要括號。不要使用影響內置函數的變量(即'sum')。不要讓你的變量大寫開始 - 良好的風格有變量開始小寫,類開始大寫。我強烈推薦看一下[Python風格指南](http://www.python.org/dev/peps/pep-0008/#introduction)。 – thegrinner

+0

感謝grinner! –

回答

3

那些布爾值是不需要的。您可以到因子代碼:

while True: 
    try: 
     num = int(input("Please enter a number: ")) # Note how I've added int() 
     break # Breaks out of the loop. No need for a boolean. 
    except ValueError: 
     print("Wrong input. Try again.") 

我不明白爲什麼你叫list(map(int, str(num)));但我認爲你打算把int()圍繞你的輸入。所以我在上面添加了一個。現在它可以捕獲一個錯誤:)。

現在,得到一個數字,你可以在這裏使用另一while循環:

while num > 9: 
    num = sum(map(int, str(num))) 

差不多這個創建[1, 0, 0]sum()然後調用。重複此過程,直到它不再是一個兩位數字(或三,四等)

這麼幹脆:

while True: 
    try: 
     num = int(input("Please enter a number: ")) # Note how I've added int() 
     break # Breaks out of the loop. No need for a boolean. 
    except ValueError: 
     print("Wrong input. Try again.") 

while num > 9: # While it is a two digit number 
    num = sum(map(int, str(num))) 

只是注意,條件語句,它從來沒有Python的做a == Trueb == False

PEP

不要比較爲真或假使用==布爾值。

是:如果問候:

編號:如果問候==真:

更糟的是:如果問候爲true:

+0

這不會回答輸入的問題:「12399」 – dav1d

+0

@ dav1d帶引號?那麼它不應該... – TerryA

+0

我認爲dav1d是正確的(引號旁)。 – RussW

1

你非常接近。下面是你需要改變什麼:

Sum=sum(ListOfDigits) 
while(Sum>9): 
    Sum=sum(ListOfDigits) 
    if (Sum>9): 
     ListOfDigits=list(map(int,str(Sum))) 
     Sum=sum(ListOfDigits) 

在這段代碼中,你有一個while循環執行時sum是大於9。那麼爲什麼要使用另一個變量Sum(也,它使真的很難閱讀碼)?取而代之:

while(sum>9): 
    sum=sum(ListOfDigits) 
    ListOfDigits=list(map(int,str(sum))) 

這只是爲了向您說明您的代碼出了什麼問題。我不會推薦使用它(看下面我會做什麼)。首先,你混合使用可變命名約定,這是一個非常糟糕的主意,尤其是當你在一個團隊中工作時(甚至可以想象在一個月或六個月後查看你的代碼?)。
二,你永遠不會使用goodvalue2;那裏有什麼?
三,如果goodvalue1只會是bool,那爲什麼要檢查if (goodvalue1==True)if goodvalue1更清晰,更pythonic。
請爲了所有好的愛,在你的代碼中使用一些空格。在看了像ListOfDigits=list(map(int,str(num)))這樣的表情一段時間後,眼睛變得非常緊張。改爲嘗試ListOfDigits = list(map(int, str(num)))

就個人而言,我會做到這一點:

num = None 
while num is None: 
    try: 
     num = int(raw_input("Enter a number: ")) 
    except ValueError: 
     num = None 

num = sum(int(i) for i in str(num)) 
while num > 9: 
    num = sum(int(i) for i in str(num)) # this uses a list comprehension. Look it up, they're very useful and powerful! 
+0

這段代碼很糟糕,但仍然正確,爲什麼downvote? – dav1d

+0

@ dav1d:謝謝。我想知道同樣的事情 – inspectorG4dget

+1

@ dav1d:嘗試運行它,然後回來告訴我們它有多正確。它嘗試調用被局部變量遮蔽的內建函數,並建議撤銷代碼中那種試圖通過更改大寫來停止對其進行遮蔽(授予,失敗)的代碼部分。 – geoffspear

-1

遞歸下去!

計算數字的總和。檢查總和是否有一位數字或多位數字。如果一個數字,那就是你的答案,否則,再次調用函數。

def oneDigitSum(n): 
    if n < 10: 
     return n 
    else: 
     return oneDigitSum(sum([int(i) for i in str(n)])) 
     # [f(elem) for elem in li] = [f(a), f(b), .... ] where li = [a, b, ... ] 
     # sum returns the total of the numbers in list 

while True: # continue this loop for eternity, until it gets break 
    try: 
     num=int(input("Please enter a number: ")) 
     print(oneDigitSum(num)) 
     break # printed the sum, now I can break the loop peace fully 
    except ValueError: 
     print ("Wrong input. Try again.") 
     continue # oops, looks like wrong input, lets continue the loop 
+0

誰在downvoting? – rnbcoder

+0

如果您打算使用列表推導來回答似乎是Python新手的用戶,請解釋它是什麼。他們對於新人的理解並不是微不足道的。 – thegrinner

+0

我在添加此代碼之前就已經downvoted了。 – rnbcoder

1

我拿到這個:

inp = None 
while inp is None: 
    try: 
     inp = int(input('Enter number here: ')) 
    except ValueError: 
     print('Invalid Input, try again') 

summed = sum(map(int, str(inp))) 
while summed > 9: 
    summed = sum(map(int, str(summed))) 

print('The result is {}'.format(summed)) 

對於一個解釋@Haidro做得很好:https://stackoverflow.com/a/17787707/969534

相關問題