简体   繁体   English

Python Napier计算器问题

[英]Python Napier Calculator Issue

So, I've been working at this for hours and hours, this is a homework assignment and I just can't figure out why the code doesn't execute completely. 所以,我已经工作了好几个小时,这是一项家庭作业,我只是想不出为什么代码不能完全执行。 I provided all the code to see if there was something I missed outside of the 'assign2' function. 我提供了所有代码,以查看在“ assign2”函数之外是否缺少某些内容。 However, I know the issue is down there and want to figure out what's wrong. 但是,我知道问题出在那儿,想找出问题所在。

I'm essentially trying to take the number that is generated last and turn it back into letters representative of Napier arithmetic (ie a = 0, b = 1, c = 2...z = 25) and putting them together in a list that I can print in the main function. 我本质上是在尝试获取最后生成的数字,然后将其转换回代表纳皮尔算术的字母(即a = 0,b = 1,c = 2 ... z = 25)并将它们放到一个列表中我可以在主要功能中打印 Everything else works except for this last part and I'm trying to figure out why. 除了最后一部分,其他所有内容都可以使用,我正在尝试找出原因。

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()

Your function is fairly close. 您的功能相当接近。 The problem is with ch = chr(value + ord("a")) . 问题出在ch = chr(value + ord("a")) We need to encode the bit position into the letter with that position in the alphabet. 我们需要将位位置编码为字母,并在字母表中具有该位置。 A letter gets added to the list if the bit in that position is not zero. 如果该位置的位不为零,则将字母添加到列表中。 And at the end of the function we can join the list of letters into a string. 在函数的结尾,我们可以将字母列表连接成一个字符串。

Here's a repaired version of your function, with some test code that verifies that it works on the examples in the Wikipedia article on Location_arithmetic 这是功能的修复版本,带有一些测试代码,可以验证该功能是否可以在Wikipedia上有关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)

output 产量

87 abceg abceg True
3147 abdgkl abdgkl True

Here's a more Pythonic version of that function, with a more meaningful name. 这是该函数的更多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)

And here's another one that avoids the character calculation by looping over a string containing the lowercase letters. 这是另一个通过循环包含小写字母的字符串来避免字符计算的方法。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM