[英]How would you take a value from a list and assign it to a variable
只是想知道如何从滚动列表中获取一个值并将其分配给一个变量,以便在其余代码中使用。
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)
已死@ user3119844。
我认为您的问题有点奇怪,因为它与当前的代码不一致。
通过代码,您似乎是一位相当熟练的python程序员。 因此,要么我不了解这个问题,要么这很奇怪。
即使在我看来,rolls字典也包含3个列表(与3个不同的骰子有关),每个列表都包含一些值。 在python中,您可以使用[](括号)通过其索引访问值。 因此,选择一个变量名,例如说v
,然后:
v=rolls[diceNumber][rollIndex]
其中diceNumber
是4,6或12,而rollIndex
是您感兴趣的第n个卷的编号。
希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.