对不起,如果这很明显,我很新。
这是代码。它不应该打印与我理解的相同的两件事,但有时会打印。关键是 p1 为 1 应防止 p2 为 1,如果 p2 为 1,则 p2 应以相同的 p1 值再次运行,但应生成一个新的随机数。它可能又是 1,但是函数应该继续返回 else 并运行直到它们不同,对吧?
#Random Test with Exclusion
P1Item = 'Empty'
P2Item = 'Empty'
import random
import time
def P1():
global P1Item
global P2Exclusion
P1Local = random.randint(1,3)
if P1Local == 1:
P1Item = 'Candy'
P2(P1Local)
elif P1Local == 2:
P1Item = 'Steak'
P2(P1Local)
elif P1Local == 3:
P1Item = 'Vegetables'
P2(P1Local)
def P2(A):
global P2Item
P2Local = random.randint(1,3)
if P2Local == 1 and A != 1:
P2Item = 'Candy'
elif P2Local == 2 and A != 2:
P2Item = 'Steak'
elif P2Local == 3 and A != 3:
P3Item = 'Vegetables'
else:
B = A
P2(B)
def Test():
print('Test')
print('Define')
P1()
print(P1Item + ' ' + P2Item)
time.sleep(1)
input()
Test()
Test()