为自制计算器添加功能
adding functions to self made calculator
好的,所以我正在尝试制作一个可用的 napier 数字计算器。我已经完成了让它工作所需的大部分步骤。我需要再执行 2 个步骤才能将其转换回 napier 数字。目前我一直在努力让这些功能发挥作用。它似乎只是跳过了那一步。据我所知,它应该有效并且不会被跳过。谁能告诉我我是否错过了制作该功能过程中的一个步骤。
def main():
response = 'y'
while response == 'y' or response == 'Y':
nap1 = getNapier()
num1 = napToInt(nap1)
print(num1)
nap2 = getNapier()
num2 = napToInt(nap2)
print(num1, num2)
operator = getOperator
result = doMath(num1, num2, operator)
response = input("Try another[y/n]")
def doMath(num1, num2, operator):
if operator == "+":
answer = num1 + num2
elif operator == "-":
answer = num1 - num2
elif operator == "*":
answer = num1 * num2
else:
if operator == "/":
answer = num1 / num2
return doMath
def getOperator():
op = input("Enter operator: ")
while op not in "+-*/":
op = input("Error!!!! Enter operator: ")
return op
def napToInt(n):
result = 0
for ch in n:
result += 2 ** (ord(ch) - ord('a'))
return result
def getNapier():
nap = input("Enter Napier number: ")
while not nap.isalpha():
nap = input("Error!!! Enter Napier number: ")
return nap
main()
这是我得到的结果,如您所见,它得到了 napier 数字,然后就停止了
Enter Napier number: asdf
262185
Enter Napier number: adsf
262185 262185
Try another[y/n]
你的行 operator = getOperator
应该是 operator = getOperator()
好的,所以我正在尝试制作一个可用的 napier 数字计算器。我已经完成了让它工作所需的大部分步骤。我需要再执行 2 个步骤才能将其转换回 napier 数字。目前我一直在努力让这些功能发挥作用。它似乎只是跳过了那一步。据我所知,它应该有效并且不会被跳过。谁能告诉我我是否错过了制作该功能过程中的一个步骤。
def main():
response = 'y'
while response == 'y' or response == 'Y':
nap1 = getNapier()
num1 = napToInt(nap1)
print(num1)
nap2 = getNapier()
num2 = napToInt(nap2)
print(num1, num2)
operator = getOperator
result = doMath(num1, num2, operator)
response = input("Try another[y/n]")
def doMath(num1, num2, operator):
if operator == "+":
answer = num1 + num2
elif operator == "-":
answer = num1 - num2
elif operator == "*":
answer = num1 * num2
else:
if operator == "/":
answer = num1 / num2
return doMath
def getOperator():
op = input("Enter operator: ")
while op not in "+-*/":
op = input("Error!!!! Enter operator: ")
return op
def napToInt(n):
result = 0
for ch in n:
result += 2 ** (ord(ch) - ord('a'))
return result
def getNapier():
nap = input("Enter Napier number: ")
while not nap.isalpha():
nap = input("Error!!! Enter Napier number: ")
return nap
main()
这是我得到的结果,如您所见,它得到了 napier 数字,然后就停止了
Enter Napier number: asdf
262185
Enter Napier number: adsf
262185 262185
Try another[y/n]
你的行 operator = getOperator
应该是 operator = getOperator()