Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python创建一个基数为12的计算器,在不同的数字处具有不同的限制_Python_Math_System_Calculator - Fatal编程技术网

使用python创建一个基数为12的计算器,在不同的数字处具有不同的限制

使用python创建一个基数为12的计算器,在不同的数字处具有不同的限制,python,math,system,calculator,Python,Math,System,Calculator,我想创建一个计算器,可以对12进制的数字进行加法、乘法、除法等运算,并且在不同的数字上有不同的限制 text = "14:54:31" # some time # convert each int of the time into base12 - join with | again base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":")) # split base12 at |, con

我想创建一个计算器,可以对12进制的数字进行加法、乘法、除法等运算,并且在不同的数字上有不同的限制

text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
碱基12序列:[0,1,2,3,4,5,6,7,8,9,A,B]

text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
限制必须是:

text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
第一位:限制B 第二位:限制4 第三位:限制B

text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
其想法是,它遵循每小时系统限制,但在基数12中,例如在基数12中,一分钟有50秒

text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
这意味着你会这样计算:[1,2,3,4,5,6,7,8,9,A,B,10,11,…48,49,4A,4B,100101,…14B,200201,…B4B,10001001..]

text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
所以我做了以下代码

 import string
digs = string.digits + string.ascii_uppercase


def converter(number):
    #split number in figures
    figures = [int(i,12) for i in str(number)]
    #invert oder of figures (lowest count first)
    figures = figures[::-1]
    result = 0
    #loop over all figures
    for i in range(len(figures)):
        #add the contirbution of the i-th figure
        result += figures[i]*12**i
    return result

def int2base(x):
    if x < 0:
        sign = -1
    elif x == 0:
        return digs[0]
    else:sign = 1

    x *= sign
    digits = []

    while x:
        digits.append(digs[int(x % 12)])
        x = int(x / 12)

    if sign < 0:
        digits.append('-')

    digits.reverse()

    return ''.join(digits)

def calculator (entry1, operation, entry2):
    value1=float(converter(entry1))
    value2=float(converter(entry2))
    if operation == "suma" or "+":
        resultvalue=value1+value2
    else:
        print("operación no encontrada, porfavor ingrese +,-,")
    result=int2base(resultvalue)
    return result


print(calculator(input("Ingrese primer valor"), input("ingrese operación"), input("Ingrese segundo valor")))
text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
问题是我不知道如何确定不同数字的限制
如果有人能帮助我,我会非常出色

您可以定义两个转换器:

class Base12Convert:
    d = {hex(te)[2:].upper():te for te in range(0,12)}
    d.update({val:key for key,val in d.items()})
    d["-"] = "-"

    @staticmethod
    def text_to_int(text):
        """Converts a base-12 text into an int."""
        if not isinstance(text,str):
            raise ValueError(
                f"Only strings allowed: '{text}' of type '{type(text)}' is invalid")
        t = text.strip().upper()
        if any (x not in Base12Convert.d for x in t):
            raise ValueError(
                f"Only [-0123456789abAB] allowed in string: '{t}' is invalid")            
        if "-" in t.lstrip("-"):
            raise ValueError(f"Sign '-' only allowed in front. '{t}' is invalid")
        # easy way
        return int(t,12)

        # self-calculated way
        # return sum(Base12Convert.d[num]*12**idx for idx,num in enumerate(t[::-1]))

    @staticmethod
    def int_to_text(num):
        """Converts an int into a base-12 string."""

        sign = ""
        if not isinstance(num,int):
            raise ValueError(
                f"Only integer as input allowed: '{num} of type {type(num)}' is invalid")
        if num < 0:
            sign = "-"
            num *= -1

        # get highest possible number
        p = 1
        while p < num:
            p *= 12

        # create string 
        rv = [sign]
        while True:
            p /= 12
            div = num // p
            num -= div*p
            rv.append(Base12Convert.d[div])
            if p == 1:
                break

        return ''.join(rv)
text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
输出:

text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")
14:54:31
12|46|27
14|54|31

并使用普通整数对数字实施任何限制。你应该把时钟的数字分开。。。以10为基数的14b 203没有意义,如果您的意思是1:59小时/分钟,那么1:4b可能是有意义的。

所以是关于修复代码,而不是实现您的想法。请反复阅读,如果您有问题,请提供您的代码。如果遇到错误,请将错误消息逐字复制并粘贴到问题中。你的问题更适合导师、老师或一些有经验的同龄人。我们这里不做家教——这不是SO的目的。我修正了它,并提出了我的确切疑问,我认为现在它更像是一种幻觉。谢谢你的评论,我是新来的
text = "14:54:31"  # some time 

# convert each int of the time into base12 - join with | again
base12 = '|'.join(Base12Convert.int_to_text (int(t)) for t in text.split(":"))

# split base12 at |, convert to int, make str from int and join with | again
base10 = '|'.join(map(str,(Base12Convert.text_to_int (b12) for b12 in base12.split("|"))))

# print all 3
print(text,base12,base10,sep="\n")