首页 > 解决方案 > 遍历字符串时,我的结果中出现了一个从未给出的字符

问题描述

最近我一直在制作一个迭代字符串的 Python 程序。我正在用这个制作 ESOLANG,但我遇到了一个大问题。 这个问题很长,对此我深表歉意。 这是基本语法,您需要知道它才能回答这个问题。

from math import factorial
def parse(code, currentValue=0, varis={"?":"2"}, getCurrent=False):
    # Copyright (c) UCYT5040
    # minlang - A minimal language
    output = ""
    outputNext = False
    getNext = False
    setNext = False
    valNext = False
    appendNext = False
    getLoopAmount = False
    inLoop = False
    loopCode = ""
    runMoreTimes = 0
    openLoops = 0
    for i in code:
        if valNext:
            currentValue = ord(i)
            valNext = False
            continue
        if appendNext:
            currentValue += ord(i)
            appendNext = False
            continue
        if getNext:
            if i == "c":
                currentValue = ord(varis[chr(currentValue)])
            else:
                currentValue = ord(varis[i])
            getNext = False
            continue
        if outputNext:
            output += chr(i)
            outputNext = False
            continue
        if setNext:
            varis[i] = chr(currentValue)
            setNext = False
            continue
        dontDoThis = False
        if inLoop:
            if i == "(": openLoops+=1
            if i == ")" and openLoops != 0:
                openLoops -= 1
            if openLoops == 0:
                inLoop = False
                dontDoThis = True
            if not dontDoThis:
                loopCode += i
                continue
        if getLoopAmount:
            if i == "c": runMoreTimes = currentValue
            else: runMoreTimes = int(i)
            getLoopAmount = False
            inLoop = True
        while runMoreTimes > 0:
            if not inLoop:
                runMoreTimes -= 1
                output += parse(loopCode, currentValue, varis)
                currentValue = parse(loopCode, currentValue, varis, getCurrent=True)
            else: break
        if runMoreTimes == 0 and not inLoop:
            loopCode = ""

        if i == "e": # Sets the current value to the next character
            valNext = True
        if i == "a": # Adds the next character to the current value
            appendNext = True
        if i == "p": # Print current value
            output += chr(currentValue)
        if i == "g": # Get the value of a variable
            getNext = True
        if i == "v": # Set a variable
            setNext = True
        if i == "(":
            getLoopAmount = True
            openLoops += 1
        if i == "-":
            currentValue -= 1
        if i == "+":
            currentValue += 1
        if i == "i":
            currentValue = ord(input("Enter only 1 character of input: "))
        if i == "?":
            if varis["%"] == varis["!"]:
                output += chr(currentValue)
        if i == "&":
            if not varis["%"] == varis["!"]:
                break
        if i == "f":
            fact = str(factorial(currentValue))
            varis["0"] = str(len(fact))
            for ii in range(len(fact)):
                f = fact[ii]
                print(type(f),f,"fact")
                varis[str(ii+1)] = f
            print(varis["0"])
    if getCurrent:
        return currentValue
    return output
print(parse(input("Enter code to run: ")))

这是我的一些尝试,以使其正常工作。

UCYT5040: min run +++++fg1pg2pg3p 
BOT > Minlang Runner: 120

这是成功的,但以下不是。

UCYT5040: min run va++++f g0(cga+vagcp)
Command raised an exception: KeyError: '\x01'
Expanation: Set var a to 0. Get the factorial of 4. Get the value of 0, and run a loop of its length. Edit var a to get each digit. Get the value of the 1st digit (var 1). --ERROR: Trying to get var \x01 rather than 1.--

为什么 \x01 通过了?

标签: pythonloops

解决方案


我认为您需要在函数的分支中用简单的and替换chr()and ,即ord()str()int()getNextif

currentValue = ord(varis[chr(currentValue)])

原因:使用currentValue = 1并假设阶乘已计算

chr(1) = '\x01'因此,当您尝试访问varis['\x01']字典中不存在的密钥时[因为在阶乘中您使用str(ii+1)...not存储它chr()

相反, str(1) = '1', 所以您将访问varis['1'],这将导致'2'and int('2') = 2


推荐阅读