打印(用于bob)仅打印出1

2024-09-27 21:26:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python编写一个脚本:

import random
import numpy as np
from operator import itemgetter
equal_to = {"a":"u_plus", "b":"u_minus", "c":"v_plus", "d":"v_minus"} #bell state
count = 0 #count until b is found
count1 = 0 #count for nested loop

p = 8 #length of generated binary
key1 = [] #list of generated binary 
for i in range(p): 
    temp = random.randint(0,1)
    key1.append(temp) 

tmplist2 = [] #list of random sample_letters
while True:
    attempt = str(random.choice(list(equal_to)))
    tmplist2.append(attempt)

    if attempt == 'b':
        break

    count += 1

#evaluate result of alice binary and bell state
def eval(alice, bell):
    if alice == 1:
        if bell == 'a' or 'b':
            return 1
        elif bell == 'c' or 'd':
            return 0
    elif alice == 0:
        if bell == 'c' or 'd':
            return 1
        elif bell == 'a' or 'b':
            return 0
for_bob = [] #list of generated binary and bell state through logic gate
for k in key1:
    for t in tmplist2:
        e = eval(k, t)
        for_bob.append(e)
    count1 += 1
#tr = [[eval(k,t) for t in tmplist2] for k in key1] #list comprehension split the key properly
print("generated random binary strings:", key1)
print("generated bell states:", tmplist2)
**print(for_bob)**

在最后一行print(for_bob),它只打印了1,尽管我把它作为函数eval()的参数(参见第三块)。当alicebell的条件等于给定条件时,它应打印1或0。我的密码有错误吗


Tags: orofinforifcountevalrandom
1条回答
网友
1楼 · 发布于 2024-09-27 21:26:10

将此粘贴到您的评估中,它将解决您的问题

#evaluate result of alice binary and bell state
def eval(alice, bell):
    if alice == 1:
        if bell == 'a' or bell == 'b':
            return 1
        elif bell == 'c' or bell == 'd':
            return 0
    elif alice == 0:
        if bell == 'c' or bell == 'd':
            return 1
        elif bell == 'a' or bell == 'b':
            return 0

“or”类似于测试变量的countinue,而不是值
,因此只需在第二个参数之前添加变量bell ==

相关问题 更多 >

    热门问题