2014-01-23 20 views
0

我對python很陌生,我想創建一個二進制到十進制轉換器和一個小數到二進制轉換器。Python中的二進制和Denary轉換器

然而二進制數的用戶要轉換隻能是八位長,必須是一個有效的二進制數,而用戶想要轉換隻能是積極的deciamal數量和最多255

我想出了這個代碼,我堅持'然而'部分。

import time 

def program(): 
    a = input ("Would you like to convert Denary To Binary (D) or Binary To Denary (B)? ") 

if a == ("D") : 

    def denary(): 
     print("The denary number you want to convert, can only be a positive number up to 255") 
     time.sleep(2) 
     e= int(input("What number would you like to convert into Binary? ")) 
     if e < 255 or e==255 or e >= 0: 
      print(bin(e)[2:].zfill(8)) 
      again=int(input("Would you like to go again YES[1] NO[2]")) 
      if again==(1): 
       program() 
      else: 
       print ("Thank you for using the program")  

     else: 
      denary() 


    denary() 




elif a == ("B"): 

    def binary(): 
     print("The binary number you want to convert, can only be eight digits long and can only be a valid binary, number consiting of 0's and 1's") 
     time.sleep(2)  
     c = int(input("What Binary number would you like to convert into Denary? ")) 
     if len(c) >8 and c== '0' or '1': 
      convert= lambda b: str(int(b, 2)) 
      print(c + " is " + convert(c) + " in Denary") 
      again=int(input("Would you like to convert a number again YES[1] NO[2]")) 
      if again==(1): 
       program() 

      else: 
       print ("Thank you for using the program") 

     else: 
      binary() 

    binary()  


else: 
    program() 

program() 
+0

可能是一個愚蠢的問題,但什麼是'Denary'? – inspectorG4dget

+0

@ inspectorG4dget base 10 – woozyking

+0

小數的另一個詞,這就是我的老師使用。 – Muhammed

回答

1
 if e < 255 or e==255 or e >= 0: 

你不想or這裏。如果至少有一個條件爲真,則將採用此分支。 1000滿足e >= 0,所以1000通過支票。 -1000滿足e < 255,所以-1000通過支票。事實上,每一個數字都會經過這裏。你想

if e >= 0 and e <= 255: 

,或者使用Python的比較鏈接,

if 0 <= e <= 255: 

 if len(c) >8 and c== '0' or '1': 

這是沒有意義的。

  • 首先,c已經是int;你試圖把它作爲一個字符串來操作。

  • 其次,長度測試不應該檢查長度更大比8.應當== 8如果你需要若有長度精確匹配或<= 8多達8個作品。

  • 第三,and之後的部分是沒有意義的。 x == y or z不測試x是否等於yz中的一個。它被解釋爲(x == y) or z,這通常是無稽之談。

  • 最後,即使c== '0' or '1'測試c是否的'0''1'之一,仍然不會是一件有意義的事情要做。如果你想測試在c的所有字符是否是'0''1',可以使用all與發電機表達式:

    所有(在(「0」,「1」碳)在C炭)

解決所有這些問題,我們有以下幾點:

c = input("What Binary number would you like to convert into Denary? ") 
if len(c) == 8 and all(char in ('0', '1') for char in c): 
+0

非常感謝,這種感覺只是一個初學者編程:) – Muhammed

1

這裏是基本的轉換功能,應該讓你去:

def dec2bin(N): 
    if not N: 
     return '' 
    else: 
     return dec2bin(N//2) + str(N%2) 

def bin2dec(b): 
    answer = 0 
    for char in b: 
     answer *= 2 
     answer += int(char) 
    return answer 

def program(): 
    answer = input("Do you want to convert decimal to binary (D) or ...: ") 
    if answer == "D": 
     N = int(input("Enter your number: ")) 
     print("The binary of %s is %s" %(N, dec2bin(N))) 

    # remainder of the UI logic goes here 
+0

雖然這可能會有幫助,但OP的真正問題是布爾邏輯錯誤。 – user2357112

+0

我很抱歉,即時通訊新的python。我應該如何執行你告訴我的計劃? – Muhammed

+0

使用這兩個功能。然後,當您需要在二進制和denary之間進行轉換時,請調用函數來執行轉換。其餘的是要求用戶輸入,你已經做了正確的輸入。隨意問更多 – inspectorG4dget

0

槓桿的內建!

月至斌:

def to_bin(n): #assuming n is a str 
    """If the str n represents a number in the range [0, 256), return a 
    binary string representation of that number, otherwise raise a ValueError 

    """ 
    if 0 <= int(n) < 256: # if n is in the valid range 
     return bin(int(n))[2:] #builtin :p 
    else: 
     raise ValueError('number must be in range [0, 256)') 

倉至十二月

def to_dec(n): 
    """If the str n is at most 8 characters long and consists only of '0' and 
    '1' characters, return the decimal string representation of it, otherwise 
    raise a ValueError 

    """ 
    if len(n) <= 8 all(c in ('0', '1') for c in n): 
     return str(int(n, 2)) 
    else: 
     raise ValueError('binary number must be <= 8 digits and consist of 0 and 1') 
+0

另一種做我的程序的方式,感謝您的時間和精力:) – Muhammed

相關問題