Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试在循环中存储信息_Python_Loops_Store - Fatal编程技术网

Python 尝试在循环中存储信息

Python 尝试在循环中存储信息,python,loops,store,Python,Loops,Store,我试着重新掷多个骰子,每次都要记住上一次的重新掷骰。例如,如果我掷5个骰子,得到1,2,3,4,5。我问你想重新掷哪个骰子-1,3,4然后得到3,2,5,3,5。但正如我在循环中所问的,它覆盖了以前的新卷,只给出了最后一卷。当我运行循环时,如何存储新找到的数字 reroll1=INPUT是否要重新回滚?是或否: 如果reroll1==是或是: 计数=0 times=intinputh您希望重新掷多少骰子? 计数

我试着重新掷多个骰子,每次都要记住上一次的重新掷骰。例如,如果我掷5个骰子,得到1,2,3,4,5。我问你想重新掷哪个骰子-1,3,4然后得到3,2,5,3,5。但正如我在循环中所问的,它覆盖了以前的新卷,只给出了最后一卷。当我运行循环时,如何存储新找到的数字

reroll1=INPUT是否要重新回滚?是或否: 如果reroll1==是或是: 计数=0 times=intinputh您希望重新掷多少骰子? 计数<次时: whicheroll=inputerroll管芯: 如果whicheroll==1: reroll1=random.randint1,6 其他: 重滚1=重滚1 如果whicheroll==2: reroll2=random.randint1,6 其他: 重滚2=重滚2 如果whicheroll==3: reroll3=random.randint1,6 其他: 重滚3=重滚3 如果whicheroll==4: reroll4=random.randint1,6 其他: 重滚4=重滚4 如果whicheroll==5: reroll5=random.randint1,6 其他: 重滚5=重滚5 新闻集=[reroll1,reroll2,reroll3,reroll4,reroll5] 计数+=1 印刷新闻集
你可以通过在骰子滚动块上循环,而不是使用所有的if语句来清理很多东西。这是通过使用所选骰子索引现有骰子卷列表来完成的。我假设你们已经有了一个包含原始骰子卷的列表,所以我只是做了一个并复制到了newset

oldset = [1,2,3,4,5]
reroll1 = str(raw_input("Would you like to reroll? Yes or No: "))
if reroll1.lower() == "yes":
    count = 0
    times = int(input("How many die would you like to reroll? "))
    newset = oldset

    while count < times:
        whichreroll = input("Reroll die: ")
        reroll = random.randint(1,6)
        newset[whichreroll-1]= reroll
        count += 1
        print(newset)  

如果我没弄错你的问题,你可以简单地通过两组列表来实现,一组是你已经拥有的一组,即新闻集,另一组是前一组。prevSet存储结果的最后一个值,因此基本上您可以在每次迭代开始时初始化prevSet,以便

while (count < times):
    prevSet = newset 
    .
    .
    .

这是什么语言?python伪代码?请添加一个标记和/或写下它是哪种语言。仅供参考:如果reroll1==是或是:不做您认为它做的事情。如果reroll1.lower==yes:您应该试试。您的问题不是很清楚。如果你输入4,你想让最后一个重播4挂起吗?或者如果您输入4,您希望重新滚动1、2、3、5以保持其以前的滚动?而i