我對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()
可能是一個愚蠢的問題,但什麼是'Denary'? – inspectorG4dget
@ inspectorG4dget base 10 – woozyking
小數的另一個詞,這就是我的老師使用。 – Muhammed