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()
這是你正在談論的[Napier算術](https://en.wikipedia.org/wiki/Location_arithmetic)嗎? –
是的,就是那 –
我們並不需要看到你的整個程序。問題代碼應該是專注於您的問題的[mcve]。所以在這種情況下,我們只需要看到'assign2'的代碼,其中包含一些樣例輸入,預期輸出和實際輸出。 –