首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python中将base Shadocks更改为Base 10

在Python中将base Shadocks更改为Base 10
EN

Stack Overflow用户
提问于 2018-02-21 00:43:49
回答 1查看 54关注 0票数 0

我有一个小问题谁阻止我,我有一个工作,我必须转换一个数字到Shadocks (基数4似乎),我必须使一个解密器。所以我做了第一部分,但我的代码在第二部分不起作用。如下所示:

代码语言:javascript
复制
def Base10toShadocks(n):
    q = n
    r = 0
    Base4=[]
    Shads=["GA","BU","ZO","MEU"]
    if q == 0:
        Base4.append(0)
    else:
         while q > 0:
             q = n//4
             r = n%4
             n = q
             Base4.append(r)
         Base4.reverse()
         VocShad = [Shads[i] for i in Base4]
    print(VocShad)
def ShadockstoBase10(n):
    l=len(n)
    Erc_finale=[]
    for i in range(l):
        Sh=(n[i])
        i=i+1
        if Sh =="a":
            Erc_finale.append(0)
        elif Sh =="b":
            Erc_finale.append(1)
        elif Sh =="o":
            Erc_finale.append(2)
        elif Sh =="e":
            Erc_finale.append(3)
     print(Erc_finale)
     F=str(Erc_finale)
     print(F)
     F=F.replace("[","")
     F=F.replace("]","")
     F=F.replace(",","")
     F=F.replace(" ","")
     L2=len(F)
     F=int(F)
     print(L2)
     print(F)
     r=0
     while f < 4 or F ==4:
        d=(F%4)-1
        F=F//4
        print(d)
        r=r+d*(4**i)
        print(r)

inp = 0
inp2 = 0
print("Tapez \"1\" pour choisir de traduire votre nombre en shadock, ou \"2\" pour inversement")
inp = int(input())
if inp == 1:
    print("Quel est le nombre ?")
    inp2 = int(input())
    if inp2 != None:
        Base10toShadocks(inp2)
elif inp == 2:
    print("Quel est le nombre ?")
    inp2 = str(input())
    if inp2 != None:
        ShadockstoBase10(inp2)

它在F=int(F)阻塞,我不明白为什么。谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-21 02:35:36

首先,您的代码中存在一些错误:

代码语言:javascript
复制
for i in range(l):
    Sh=(n[i])
    i=i+1      #### Won't work. range() will override
    ##### Where do "a","b","o","e" come from
    ##### Shouldn't it be "G","B","Z","M" ("GA","BU","ZO","MEU")?
    if Sh =="a":
        Erc_finale.append(0)
    elif Sh =="b":
        Erc_finale.append(1)
    elif Sh =="o":
        Erc_finale.append(2)
    elif Sh =="e":
        Erc_finale.append(3)
 print(Erc_finale)
 F=str(Erc_finale)    ### Not how you join an array into a string

下面是一种修正的方法:

代码语言:javascript
复制
def ShadockstoBase10(n):
    n = n.upper();    # Convert string to upper case
    l = len(n)
    Erc_finale = ""   # Using a string instead of an array to avoid conversion later
    i = 0
    while i < l:      # while loop so we can modify i in the loop
        Sh = n[i:i+2] # Get next 2 chars
        i += 2        # Skip 2nd char
        if Sh == "GA":
            Erc_finale += "0"
        elif Sh == "BU":
            Erc_finale += "1"
        elif Sh == "ZO":
            Erc_finale += "2"
        elif Sh =="ME" and "U" == n[i]:
            Erc_finale += "3"
            i += 1;      # MEU is 3 chars
        else:
            break;       # bad char
    return int(Erc_finale, 4)  # Let Python do the heavy work

像Python中的所有东西一样,还有其他方法可以做到这一点。我只是试着让我的代码和你的相似。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48890152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档