2017-03-08 48 views
0

所以,我一直在這裏工作幾個小時,這是一項家庭作業,我只是無法弄清楚爲什麼代碼不能完全執行。我提供了所有代碼,以查看是否有某些我在'assign2'函數之外漏掉的東西。但是,我知道這個問題在那裏,並想弄清楚什麼是錯的。Python內皮爾計算器問題

我基本上試圖把最後生成的數字變成代表Napier arithmetic(即a = 0,b = 1,c = 2 ... z = 25)的字母並將它們放在一起在我可以在主要功能中打印的列表中。除了最後一部分,其他所有部分都可以工作,我正試圖弄清楚爲什麼。

def main(): 
    again = "y" 
    while again == "y" or again == "Y": 
    var = checkalpha() 
    num = assign(var) 
    print("The first number is: {}".format(num)) 
    var2 = checkalpha() 
    num2 = assign(var2) 
    print("The second number is: {}".format(num2)) 
    arithmetic = getsign() 
    value = equation(num, num2, arithmetic) 
    newvar = assign2(value) 
    print("The result is {} or {}".format(value, newvar)) 
    again = input("Would you like to repeat the program? Enter y for yes, n for no: ") 

def checkalpha(): 
    num = input("Enter Napier number: ") 
    while not num.isalpha(): 
    print("Something is wrong. Try again.") 
    num = input("Enter Napier number: ")   
    return num 

def assign(char): 
    value = 0 
    for ch in char: 
     value += 2 ** (ord(ch) - ord("a")) 
    return value 

def getsign(): 
operand = input("Enter the desired arithmetic operation: ") 
while operand not in "+-*/": 
    operand = input("Something is wrong. Try again. ") 
return operand 

def equation(num, num2, arithmetic): 
    if arithmetic == "+": 
    answer = num + num2 
    elif arithmetic == "-": 
    answer = num - num2 
    elif arithmetic == "*": 
    answer = num * num2 
    elif arithmetic == "/": 
    answer = num/num2 
    else: 
    input("Something is wrong. Try again. ") 
    return answer 

def assign2(n): 
    new = [] 
    while n != 0: 
    value = n%2 
    x = n//2 
    ch = chr(value + ord("a")) 
    new.append(ch) 
    n = x 
    return new 

main() 
+0

這是你正在談論的[Napier算術](https://en.wikipedia.org/wiki/Location_arithmetic)嗎? –

+0

是的,就是那 –

+0

我們並不需要看到你的整個程序。問題代碼應該是專注於您的問題的[mcve]。所以在這種情況下,我們只需要看到'assign2'的代碼,其中包含一些樣例輸入,預期輸出和實際輸出。 –

回答

0

您的功能非常接近。問題出在ch = chr(value + ord("a"))。我們需要將位位置編碼成字母表中那個位置的字母。如果該位置的位不爲零,則會將一個字母添加到列表中。在函數結尾處,我們可以將字母列表連接到一個字符串中。

這是你的功能的修復版本,與驗證它適用於維基百科文章中的例子上Location_arithmetic

def assign2(n): 
    new = [] 
    position = 0 
    while n != 0: 
     value = n % 2 
     x = n // 2 
     if value: 
      ch = chr(position + ord("a")) 
      new.append(ch) 
     n = x 
     position += 1 
    return ''.join(new) 

# test 

data = [ 
    (87, 'abceg'), 
    (3147, 'abdgkl'), 
] 

for n, napier_string in data: 
    s = assign2(n) 
    print(n, napier_string, s, napier_string == s) 

輸出

87 abceg abceg True 
3147 abdgkl abdgkl True 

一些測試代碼這是一個更具Pythonic版本的函數,並帶有一個更有意義的名字。

def int_to_napier(n): 
    new = [] 
    for position in range(26): 
     if n == 0: 
      break 
     value, n = n % 2, n // 2 
     if value: 
      new.append(chr(position + ord("a"))) 
    return ''.join(new) 

這裏是另一個通過循環包含小寫字母的字符串來避免字符計算。

from string import ascii_lowercase 

def int_to_napier(n): 
    new = [] 
    for ch in ascii_lowercase: 
     if n == 0: 
      break 
     value, n = n % 2, n // 2 
     if value: 
      new.append(ch) 
    return ''.join(new)