如何解决在Python中的一段时间或任何时候循环之外获取/传输数据
我有与这2个相关的学习问题:
1:如何将一个循环(在我的情况下为while循环)中创建的数据从该循环中转移出来并存储。 2:如何重播循环几次,并使用循环中的新输入将数据更新为存储在循环外部的数据。
import random
player_input = input("how many rounds? ")
def main():
roll1 = 0
roll2 = 0
rounds = 1
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0
ten = 0
eleven = 0
twelve = 0
list1 = []
listfinal = []
while rounds <= int(player_input):
roll1 = dice_roll()
roll2 = dice_roll()
third = roll1 + roll2
if third == 2:
two = two + 1
if third == 3:
three = three + 1
if third == 4:
four = four + 1
if third == 5:
five = five + 1
if third == 6:
six = six + 1
if third == 7:
seven = seven + 1
if third == 8:
eight = eight + 1
if third == 9:
nine = nine + 1
if third == 10:
ten = ten + 1
if third == 11:
eleven = eleven + 1
if third == 12:
twelve = twelve + 1
rounds = rounds + 1
print("two =",+ two," three=",+ three," four=",+ four," five=",+ five," six=",+ six," seven=",+ seven," eight=",+ eight," nine=",+ nine," ten=",+ ten," eleven=",+ eleven," twelve=",+ twelve)
list1.append(two)
list1.append(three)
list1.append(four)
list1.append(five)
list1.append(six)
list1.append(seven)
list1.append(eight)
list1.append(nine)
list1.append(ten)
list1.append(eleven)
list1.append(twelve)
print(list1)
def dice_roll():
diceRoll = random.randint(1,6)
return diceRoll
main()
我现在的目标是在循环外创建一个列表,该列表累积任何人都想玩的每个游戏的值。 最后,我想要一个列表,将每个索引的值(两只眼睛,三只眼睛,四只眼睛...等等……十二只眼睛)存储在一个列表中。
现在的输出看起来像这样:
how many rounds? 999
two = 27 three= 56 four= 84 five= 114 six= 142 seven= 175 eight= 112 nine= 120 ten= 83 eleven= 54 twelve= 32
[27,56,84,114,142,175,112,120,83,54,32]
到目前为止,这很好,我现在想要的是使任何人都可以玩多个游戏,然后将所有数据存储在一个列表中,这是可能的。 如何管理该任务?
非常感谢。
解决方法
第1部分:如何存储数据
用于此类问题的经典数据结构是字典。您可以使用它来累积值的计数器。
# Creating an empty dictionary
my_dict = {}
# Storing/updating values in a dictionary
my_dict[key] = some_value
# Retrieving the value from a dictionary
print(my_dict[key])
>>> some_value
# initialise the dictionary with the desired values
counter = {key: 0 for key in range(2:13)}
while rounds <= int(player_input):
# Just repeating your rolling code here
roll1 = dice_roll()
roll2 = dice_roll()
third = roll1 + roll2
# Incrementing the counter
counter[third] += 1
# Will print out the key,value pairs in the dictionary
print(counter)
第2部分:如何不断循环播放:
这里的逻辑是您需要两个循环。一个循环不断要求玩家输入,而另一个循环实际上是在玩游戏来获得结果。
# setup the dictionary
counter = {key: 0 for key in range(2,13)}
# outer loop will keep asking for user input
while True:
try:
rounds = int(input('How many rounds do you want to roll the dice?'))
except TypeError:
# handle invalid input,going to break for now
print('Invalid input,exiting')
break
# Handles a 0 input as an exit instruction
if rounds == 0:
break
# loops and rolls the dice
for _ in range(rounds):
# Just repeating your rolling code here
roll1 = dice_roll()
roll2 = dice_roll()
third = roll1 + roll2
# Incrementing the counter
counter[third] += 1
最后,字典将在多轮用户输入中累积所有滚动的结果。
,好的,词典版本可与词典一起用作数据存储。但是出现了一个新问题:如何返回while循环和输入函数。
我希望用户能够玩自己想要的尽可能多的游戏,并且总是回到问题:“几轮(掷)?”。
我想存储在一个字典中的所有游戏的输出。
我只想了解它如何与python一起使用? 后来我想将数据用于统计分析和可视化,如正态分布等。
请。我不希望使用mathplot等“简单”的解决方案。我希望它尽可能基本,因为它可以使用最原始的python来了解和了解该过程。
由于词典的建议,您现在拥有的是
import random
player_input = input("how many rounds? ")
def main():
roll1 = 0
roll2 = 0
rounds = 1
two = 0
three = 0
four = 0
five = 0
six = 0
seven = 0
eight = 0
nine = 0
ten = 0
eleven = 0
twelve = 0
list1 = []
my_dict = {}
while rounds <= int(player_input):
roll1 = dice_roll()
roll2 = dice_roll()
third = roll1 + roll2
if third == 2:
two = two + 1
elif third == 3:
three = three + 1
elif third == 4:
four = four + 1
elif third == 5:
five = five + 1
elif third == 6:
six = six + 1
elif third == 7:
seven = seven + 1
elif third == 8:
eight = eight + 1
elif third == 9:
nine = nine + 1
elif third == 10:
ten = ten + 1
elif third == 11:
eleven = eleven + 1
elif third == 12:
twelve = twelve + 1
rounds = rounds + 1
print("two =",+ two," three=",+ three," four=",+ four," five=",+ five," six=",+ six," seven=",+ seven," eight=",+ eight," nine=",+ nine," ten=",+ ten," eleven=",+ eleven," twelve=",+ twelve)
new = {0: int(two),1: int(three),2: int(four),3: int(five),4: int(six),5: int(seven),6: int(eight),7: int(nine),8: int(ten),9: int(eleven),10: int(twelve)}
my_dict.update(new)
print(my_dict)
def dice_roll():
diceRoll = random.randint(1,6)
return diceRoll
main()
我现在如何一次又一次地重复同一游戏(只要需要),并将数据存储在字典外的循环中?
非常感谢大家!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。