2017-03-08 25 views
0

私はこれを何時間も働いてきましたが、これは宿題です。なぜコードが完全に実行されないのか分かりません。私は 'assign2'関数の外で見逃したことがあるかどうかを調べるためにすべてのコードを提供しました。しかし、問題はそこにあり、何が間違っているのか把握したいと思う。Python Napier Calculator Issue

私は、最後に生成された数字をとり、Napier arithmetic(すなわちa = 0、b = 1、c = 2 ... z = 25)を表す文字に戻し、私はmain関数で印刷することができます。この最後の部分を除いて他のすべてが機能し、私は理由を理解しようとしています。

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

これはあなたが話している[ネーピア算術](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 

にWikipediaの記事の例に動作することを確認するいくつかのテストコードで、あなたの関数の修理版ですより意味のある名前で、その関数の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)