Your code runs fine for me. You did not define M here so I chose an arbitrary number:
import random
def checkPizza(numOfPizza):
pizzaD = []
allowed_val = list(range(0,10))
if numOfPizza == 2:
allowed_val = allowed_val
while True:
allowed_val = allowed_val
alpha = random.choice(allowed_val)
beta = random.choice(allowed_val)
if alpha != beta:
allowed_val.remove(alpha)
allowed_val.remove(beta)
pizzaD.append(alpha)
pizzaD.append(beta)
print(pizzaD)
break
checkPizza(2)
output:
[7, 5]
It is howevery not clear for me what allowed_val=allowed_val
is suppose to do.
It appears twice in your code.
Also I think you are using a while loop for the case that alpha equals beta.
In my opinion the following would be prefered:
alpha,beta = random.sample(allowed_val,2)
This will asign two values from the list of allowed_val that are not the same.
In this case you do not need the while True
loop
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…