2014-05-10 59 views
-2

我不知道爲什麼這被打破。也不要告訴我使用python的內置函數,因爲這是爲某些練習設計的,而不是實際使用。它是二進制到十進制被打破。它與變量「指標」損壞的二進制轉換器。索引錯誤

print('Please choose from the list below:') 
    print('') 
    print('1) Convert from decimal/denary to binary; ') 
    print('2) Covert from binary to denary/decimal; ') #Main Menu 
    print('3) Infomation and settings (coming soon);') 
    print('4) Exit. ') 
    print('') 
    menu_choice = str(input('Enter your choice here: ')) #User inputs choice here 

    if menu_choice == '1': 
     dec_binary() 
    elif menu_choice == '2': 
     binary_dec() 
    elif menu_choice == '3': 
     print('By Callum Suttle') 
    else: 
     return 'Thank You' 

def dec_binary():               #Module 1: decimal to binary 
    dec = int(input('Enter a number in decimal up to 255: '))    #Checks The number is an ok size, could be made bigger   
    while dec > 255: 
     dec = int(input('Enter a number up to 255, no more: ')) 

    power = int(7)  #change 7 to make bigger by 1 digit              
    convert = [] 

    while power > -1:  #until power goes below zero 
     if dec - pow(2, power) >= 0: #if entered number subtract 2 to the power of var-pow returns posotive number 
      convert.append(1) 
      power -=1     # add a 1 
      dec = dec - pow(2, power) >= 0 
     else: 
      convert.append(0)#if not add a zero 
      power -=1 
    print('') 
    print(convert) # print completed number 
    print('') 
    binary_decimal_converter() #back to main menu 

def binary_dec(): 
    anwser = 0 
    l_bi = str(input('Enter a number in binary up to 7 digits: ')) 
    while len(l_bi) != 7: #checks for correct length 
     l_bi = str(input('Enter a number in binary up to 7 digits, you may add zeros before: ')) 
    power = 7  #size can be increased by using this 
    index = 0 
    while index > 6: #until every power has been checked (in reverse order) 
     if l_bi[index] == '1': #if the digit is equal to 1 
      anwser += pow(2, power) #add that amount 
      power -= 1 #take the power to check next           #why is this broken 
      index += 1 # add another index to check previous 
     else: 
      power -= 1 #as above 
      index += 1 #/\ 
    print('') 
    print(anwser) #prints result 
    print('') 
    binary_decimal_converter() #main menu 
+0

不完全是關係到你的問題,但這裏有設計很差。頂部的代碼是一個名爲binary_decimal_converter()的函數(我假設你在粘貼時錯過了def行)。什麼是錯誤的是,你試圖通過在每次轉換之後調用GOTO來有效地完成GOTO,但由於它實際上是一個函數調用,而不是GOTO,因此您將遞歸地堆疊函數調用。您應該將菜單置於while循環中,當選擇Exit選項時,其條件變爲false。 –

+0

你應該分開你的代碼,使其具有隻*執行轉換(沒有輸入或輸出)的函數,然後有一些接口函數來處理實際的用戶交互,並調用轉換函數。這樣,您可以單獨測試您的轉換函數,而無需一直執行所有交互邏輯。 – poke

回答

0

一些修補:

def binary_dec(): 
    anwser = 0 
    l_bi = str(input('Enter a number in binary up to 7 digits: ')) 
    while len(l_bi) > 7: # LOOP UNTIL LENGTH IS LESS THAN 7 
     l_bi = str(input('Enter... : ')) 
    power = len(l_bi) - 1 # directly set the power based on length 
    index = 0 
    while index < len(l_bi): # CORRECT LOOP CONDITION 
+0

謝謝你,更接近我的原始代碼,另一種選擇和偉大的作品 – user3258159

1

一個索引錯誤這似乎並不正確

index = 0 
while index > 6: #until every power has been checked (in reverse order) 
    ... 

你不會進入這個循環中,你呢?

更好的循環會像

for i, bit in enumerate(l_bi): 
    answer += int(bit) * pow(2, 7-i) 

還,因爲你只是在練習,你應該找到一個更好的辦法,從菜單跳到功能和背部。你正在做遞歸調用,這是一個堆棧的浪費,即你的函數實際上從來沒有完成,只是調用越來越多的函數。