私はこれを何時間も働いてきましたが、これは宿題です。なぜコードが完全に実行されないのか分かりません。私は '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()
これはあなたが話している[ネーピア算術](https://en.wikipedia.org/wiki/Location_arithmetic)ですか? –
はい、正確には –
あなたのプログラム全体を見る必要はありません。質問コードはあなたの問題に焦点を当てた[mcve]でなければなりません。したがって、この場合、 'assign2'のコードを見て、サンプル入力、期待出力、実際の出力を見るだけです。 –