2017-07-16 107 views
1
的數量

Write a function multisplit that consumes two positive integers total and split and produces the number of times total is repeatedly divided into split even pieces before each piece is of size at most 1. For example, the value returned by multisplit(8, 2) will be 3, since 8 can be split into 2 pieces of size 4, which are then each split into 2 pieces of size 2, which are then each split into 2 pieces of size 1 (at which point no further splitting takes place since the pieces are of size at most 1).計數循環

total= int(input("Total:")) 
    split= int(input("Split:")) 

    def multisplit(total,split): 
    x=o 
    while value>=1: 
     value= total//split 
     x= x+1 
    return x 

    print(x) 

一個功能它告訴我,名稱爲「x」沒有被定義

+0

它給出的* exact *消息是什麼?它是'NameError:名稱'x'未定義'?如果不是,請說明。它會發生什麼? – SethMMorton

+0

是的,這是顯示的錯誤,它出現在第11行(最後一個) –

回答

1

有與您發佈的代碼的幾個問題:

  • 在python中,函數的內容必須縮進。

    def myfunction(): 
        # code inside the function goes here 
    # code after you've unindented is not in the function 
    
  • 在使用它之前,您沒有定義您的value變量。
  • 假設您的最後一行得到適當的縮進,以便它不會被完全忽略,因爲它在函數內部,但在返回語句之後:
    您試圖打印已定義變量的值在不同的範圍內。具體來說,你在函數內部定義了x,現在你正試圖在函數外面看它。
  • 你從來沒有打電話給你的功能...
    如果我明白你想要做什麼,你想打印內部函數。即:print(multisplit(total, split))