簡體   English   中英

Python Napier計算器問題

[英]Python Napier Calculator Issue

所以,我已經工作了好幾個小時,這是一項家庭作業,我只是想不出為什么代碼不能完全執行。 我提供了所有代碼,以查看在“ assign2”函數之外是否缺少某些內容。 但是,我知道問題出在那兒,想找出問題所在。

我本質上是在嘗試獲取最后生成的數字,然后將其轉換回代表納皮爾算術的字母(即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()

您的功能相當接近。 問題出在ch = chr(value + ord("a")) 我們需要將位位置編碼為字母,並在字母表中具有該位置。 如果該位置的位不為零,則將字母添加到列表中。 在函數的結尾,我們可以將字母列表連接成一個字符串。

這是功能的修復版本,帶有一些測試代碼,可以驗證該功能是否可以在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)

產量

87 abceg abceg True
3147 abdgkl abdgkl True

這是該函數的更多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)

這是另一個通過循環包含小寫字母的字符串來避免字符計算的方法。

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