python - 遍历字符串时,我的结果中出现了一个从未给出的字符
问题描述
最近我一直在制作一个迭代字符串的 Python 程序。我正在用这个制作 ESOLANG,但我遇到了一个大问题。 这个问题很长,对此我深表歉意。 这是基本语法,您需要知道它才能回答这个问题。
- 当前值是存储为 ascii 数字的单个字符。
e
将当前值设置为下一个字符,因此eA
将当前值设置为A
并将eB
其设置为B
。p
打印当前值,所以eFpeopp
打印Foo
.v
设置一个变量。运行eAvB
会将变量设置B
为A
.g
获取变量的值。插值器带有一个?
返回版本的默认变量 ( )。所以evpg?p
会打印v2
。用于gc
获取与当前值同名的变量。(
开始一个循环并)
结束一个循环。(n)
您可以使用 n 作为 0-9 之间的数字来指定循环运行的次数。您可以使用更大的数字c
来使用当前值(ascii 数字)+
并-
增加和减少当前值。f
产生一个阶乘。该变量0
将设置为输出的长度,因此5
将设置0
为,因为3
阶乘5
是数字。然后变量-将有结果,因为有三个字母。所以这个程序将当前值设置为 5 并打印它的阶乘。. 在这个例子中,长度 (3) 是硬编码的,所以另一个数字不起作用。另一个缺陷是输入只有 1 个数字。这可以通过循环来解决。还有一些其他的字符,但它们与此无关。如果您有兴趣,可以使用这个 Discord 机器人。现在,我的问题。我试图让阶乘工作,但我无法让它工作。这是我的代码(是的,它很长):120
3
1
3
+++++fg1pg2pg3p
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 通过了?
解决方案
我认为您需要在函数的分支中用简单的and替换chr()
and ,即ord()
str()
int()
getNext
if
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
。
推荐阅读
- http - MSXML6.dll 访问被拒绝,将 HTTP 重定向到 HTTPS
- php - 作曲家安装 Symphony 时出错
- apache-beam - Apache calcite:将整数转换为日期时间
- rethinkdb - RethinkDB 查询:如何根据日期范围进行采摘
- jquery - 删除向上箭头并替换为按钮上的向下箭头
- excel - 如何从当前日期减去自动过滤 VBA 的 3 天
- c# - Unity3D中使用第一人称控制器进行空中移动,c#
- regex - 正则表达式将组视为字符串
- sql - 查找连接表中两个 ID 之间的最短路径长度
- javascript - 如何在使用 Javascript 在 div 中附加 html 时实现字符串插值