1
想知道如何從列表中獲取值並將其分配給在其餘代碼中使用的變量。如何從列表中獲取值並將其分配給變量
import random
def dice_roll(number):
if number == 12:
number = random.randint(1,12)
print(number)
return number
elif number == 6:
number = random.randint(1,6)
print(number)
return number
else:
number == 4
number = random.randint(1,4)
print(number)
return number
print("12 sided")
print("6 sided")
print("4 sided")
rolls = {4: [], 6: [], 12: []} # dictionary to hold rolls
while True:
roll = int(input("Which dice would you like to roll? --> ")) # store die size
rolls[roll].append(dice_roll(roll)) # roll and add to dictionary
doRepeat=input("Go again? --> ")
if doRepeat == "no":
break
print(rolls)
'my_var = rolls [roll] [roll_index]'?考慮到問題的模糊性,這是我能給你的最好的結果 – aruisdante