我想使用一個隨機選擇2個數字的變數,我從一個函式中獲得,并在另一個函式中使用它
def rayuela_extremo1():
n2 = random.choice([0,1])
n2 = n2
print (n2)
return n2
def rayuela_cuerpo3():
n2 = rayuela_extremo1()
if n2 == 0:
n3 = random.choice([1,3])
return n3
elif n2 == 1:
n3 = random.choice([0,2])
return n3
問題是,當我從第一個函式中獲取變數 n2 并嘗試在第二個函式中使用它時,n2 的值再次隨機選擇,因此 n2 的值在函式 rayuela_extremo1 中可能為 1,在 rayuela_cuerpo3 中可能為 0
uj5u.com熱心網友回復:
老實說,只是洗掉它沒有使用的整個功能。相反,只是做
def rayuela_cuerpo3():
n2 = random.choice([0,1])
if n2 == 0:
n3 = random.choice([1,3])
return n3
elif n2 == 1:
n3 = random.choice([0,2])
return n3
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517550.html
