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

Python 打印(用于bob)仅打印出1,python,Python,我正在用python编写一个脚本: import random import numpy as np from operator import itemgetter 在最后一行print(for_bob),它只打印了1,尽管我把它作为函数eval()的参数(参见第三块)。如果alice和bell的条件等于给定条件,则应打印1或0。我的代码有任何错误吗?将此粘贴到您的评估中,它将解决您的问题 #evaluate result of alice binary and bell state def

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

import random
import numpy as np
from operator import itemgetter

在最后一行
print(for_bob)
,它只打印了1,尽管我把它作为函数
eval()
的参数(参见第三块)。如果
alice
bell
的条件等于给定条件,则应打印1或0。我的代码有任何错误吗?

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

#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=
,eval是一个内置函数,因此我建议更改该函数名
bell=='a'或'b'
(bell=='a')或(bell=='b')
如果bell在{'a','b'}中:或者更短的
如果贝尔在'ab'中:
@Matthias-yea这也行
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)**
#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