我需要將一些列表中的數字轉換成十進制和八進制。該列表起作用,轉換也起作用。但我無法讓轉換工作在列表上。如何將列表中的數字轉換爲十進制和八進制?
print ("Welcome to Python binary convertor!")
print ("When you wish to stop entering numbers press x")
filename = "binarylist.txt"
numlist = []
num = input("Enter a binary number:")
while num !="x":
numlist.append(num)
num = input("Enter a binary number:")
def menu():
print ("Your options are:")
print (" 1. Convert Binary to Decimal")
print (" 2. Convert Binary to Octal")
print (" ")
return int(input("Choose an option"))
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
result = int(input("Enter a binary number"), 2)
print (result)
elif choice == 2:
result = int(input("Enter a binary number"), 8)
print (result)
else:
print ("Error:Invalid choice ")
loop=0
查看'oct','bin'和'hex'函數,並嘗試將它們映射到列表。例如'map(oct(numlist))'。 – HardlyKnowEm 2012-01-30 05:59:33
Aside - 使用布爾值(True/False)表示布爾檢查。這不是C,我們儘量避免這樣的垃圾!此外,而不是'loop = 0'考慮'while True:... break'結構。 – 2012-01-30 06:00:57