Python 为什么函数在循环结束和退出时在我结束后不断重复文本? 问题

Python 为什么函数在循环结束和退出时在我结束后不断重复文本? 问题,python,function,python-3.x,while-loop,try-except,Python,Function,Python 3.x,While Loop,Try Except,要重现问题,请在中运行此命令,并在提示时输入“cNOT”(不带引号)。在这之后,它应该询问你想要的第二个量子位的门,相反,它再次询问你给出的“cNOT”作为答案的相同问题 密码 将numpy导入为np def目标(qstat): typegat2=input(“这个目标量子位用于哪个门?见顶部的两个量子位门列表”) 如果类型GAT2==“cNOT”: carray=np.数组([[0,1],[1,0]]) 如果np.array_等于(mem1,[0,1])==True: 返回qstat elif

要重现问题,请在中运行此命令,并在提示时输入“cNOT”(不带引号)。在这之后,它应该询问你想要的第二个量子位的门,相反,它再次询问你给出的“cNOT”作为答案的相同问题

密码
将numpy导入为np
def目标(qstat):
typegat2=input(“这个目标量子位用于哪个门?见顶部的两个量子位门列表”)
如果类型GAT2==“cNOT”:
carray=np.数组([[0,1],[1,0]])
如果np.array_等于(mem1,[0,1])==True:
返回qstat
elif np.数组_equal(mem1[1,0])==真:
返回np.dot(qstat、carray)
其他:
打印(“叠加…未实现”)
返回qstat
其他:
打印(“尚未实现的其他门”)
返回qstat
量子位元=2
qstat={1:[0,1],2:[0,1]}
done=“n”
信号={“目标”:目标}
x=1
而x一直在说“这个目标量子位是哪个门的”
done=“y”
完成=输入(“用量子位完成?y或n:”)
其他:
打印(“对不起,该功能尚未实现,请尝试自定义门”)
done=“y”
x+=1
done=“n”
打印(“量子位结果”,1,qstat[0])
打印(“量子位结果”,2,qstat[1])
代码解释 代码被简化了,但它是一个理想量子计算机的模拟。“qstat”是每个量子位状态的字典。它可能不需要是一本字典,但它在实际程序中的方式很好,所以我保留了它。无论如何,“量子位”是系统中的量子位数,通常可以是用户输入的任何整数。函数“target”是cNOT门中的目标量子位。代码中出现“try…except”部分的原因是,有时用户会在“control”之前输入“target”,并且由于需要检查控制量子位以确定如何处理目标量子位,因此会输出一个NameError,因此我将其设置为将该量子位记录在一个列表中,我目前正在将该列表编码为“recompute”该列表中的每个量子位

“done”是一种检查用户是否完成了每个量子位的方法,由于它在while循环中,我认为在“except”语句后将其设置为“y”将结束while循环并移动到下一个量子位,但由于某种原因,它留在函数中-同一行会反复出现。如果要退出函数,必须键入一些乱码,因为这对应于最后一条else语句,如果typegat2不等于“cNOT”,则返回qstat(未更改)并继续。很明显,函数没有因为错误或其他原因返回任何内容。但问题是,如何让它退出函数


任何帮助都将不胜感激。我很乐意回答您对代码的任何问题;我试图尽可能地减少它。

摆脱
而True:
循环,这会导致每当发生
namererror
异常时代码重复

另外,将
done
的输入移动到
try
块中。否则,该输入将覆盖
块中的
done=“y”
语句,但
块除外

while x <= qubits:
    while done == "n":
        print("this is qubit #",x)
        fstgat = "target"
        print("first gate is target")
        if fstgat in singates:
            #checks if there is an error from mem1 being undefined (control/target out of order)
            try:
                qstat[x]=singates[fstgat](qstat[x])
                done = input("Done with your qubit? y or n: ")
            except NameError:
                print("switching qubits - we'll come back to this one")
                #error here -> keeps saying "which gate is this target qubit for"
                done = "y"   
        else:
            print("sorry, that functionality is not yet implemented, try custom gate")
            done ="y"
    x+=1
    done = "n"
而x一直在说“这个目标量子位用于哪个门”
done=“y”
其他:
打印(“对不起,该功能尚未实现,请尝试自定义门”)
done=“y”
x+=1
done=“n”

done=“y”
结束
而done==“n”
循环,但它不会让您退出
而True:
循环。不清楚为什么会有这样的循环。在
try
块中有
break
,因此循环仅在没有
namererror
时运行一次。看起来整个想法是在发出错误信号时重复循环。@Barmar,出于某种原因,我认为
,而True:
try…所需要的,
除外。谢谢你,我看看这是否行得通。@Barmar,行得通,如果你写下来作为回答,我会接受的。
while x <= qubits:
    while done == "n":
        print("this is qubit #",x)
        fstgat = "target"
        print("first gate is target")
        if fstgat in singates:
            #checks if there is an error from mem1 being undefined (control/target out of order)
            try:
                qstat[x]=singates[fstgat](qstat[x])
                done = input("Done with your qubit? y or n: ")
            except NameError:
                print("switching qubits - we'll come back to this one")
                #error here -> keeps saying "which gate is this target qubit for"
                done = "y"   
        else:
            print("sorry, that functionality is not yet implemented, try custom gate")
            done ="y"
    x+=1
    done = "n"