【发布时间】:2017-07-28 14:25:40
【问题描述】:
所以,我已经为此工作了好几个小时,这是一项家庭作业,我只是不明白为什么代码没有完全执行。我提供了所有代码以查看在“assign2”函数之外是否有遗漏的内容。但是,我知道问题就在那里,并想找出问题所在。
我实际上是在尝试将最后生成的数字转换回代表 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 arithmetic吗?
-
是的,就是这样
-
我们真的不需要查看您的整个程序。问题代码应该是关注您的问题的minimal reproducible example。所以在这种情况下,我们只需要查看
assign2的代码,以及一些示例输入、预期输出和实际输出。
标签: python calculator modular-arithmetic