我想使用函數(這東西,我天生不好在顯然xD),並試圖從第一個獲得返回語句信息,然後在第二個使用它。兩者都用於滾動骰子,所以第一個功能是返回第一個集合,然後在第二個函數中使用該集合,以便用戶選擇重新捲動。我在這個實現中做了什麼錯誤,它沒有認識到原始模版列表?試圖從一個函數返回信息到另一個
def rollDie():
die1 = 2
die2 = 2
die3 = 2
die4 = 4
die5 = 5
dieset = [die1,die2,die3,die4,die5]
print(dieset)
return dieset
def reRoll1():
rollDie()
reroll1 = input("Would you like to reroll? Yes or No: ")
if reroll1 == "no":
dieset = [die1,die2,die3,die4,die5]
else:
count = 0
times = int(input("How many die would you like to reroll? "))
while count < times:
whichreroll = input("Reroll die: ")
if whichreroll == "1":
die1 = random.randint(1,6)
else:
die1
if whichreroll == "2":
die2 = random.randint(1,6)
else:
die2
if whichreroll == "3":
die3 = random.randint(1,6)
else:
die3
if whichreroll == "4":
die4 = random.randint(1,6)
else:
die4
if whichreroll == "5":
die5 = random.randint(1,6)
else:
die5
dieset = [die1,die2,die3,die4,die5]
count += 1
print(dieset)
return dieset
reRoll1()
它告訴我「局部變量‘die1’分配前引用了」,但rollDie()是第一位的。如果任何人都可以解釋這對我來說將不勝感激:d
'die1','die2'寫有定義全局初始化變量,..在'rollDie'函數中定義,所以你不能在'reRoll1'函數中引用它們,請修正你的代碼格式。 – MostafaR 2013-04-08 23:16:27