Python 计数/添加+;1通过列表中的if循环。(如果某个条件为真,则添加一个元素)

Python 计数/添加+;1通过列表中的if循环。(如果某个条件为真,则添加一个元素),python,list,loops,Python,List,Loops,我想知道,既然我开始学习python,如果某个条件为真,如何创建只向给定列表添加一个元素的循环 在我的示例中,我想创建一个骰子游戏,然后收集某个值从2到12出现的次数,可以是在11个列表中,也可以是在一个列表中 我该怎么办 编辑:这里有更多的细节,以便更好地回顾我的问题和任务 import random player_input = input("how many rounds? ") def main(): roll1 = 0 roll2 = 0

我想知道,既然我开始学习python,如果某个条件为真,如何创建只向给定列表添加一个元素的循环

在我的示例中,我想创建一个骰子游戏,然后收集某个值从2到12出现的次数,可以是在11个列表中,也可以是在一个列表中

我该怎么办

编辑:这里有更多的细节,以便更好地回顾我的问题和任务

import random

player_input = input("how many rounds? ")

def main():
    roll1 = 0
    roll2 = 0 
    rounds = 1

while rounds <= int(player_input):
        roll1 = dice_roll()
        roll2 = dice_roll()
        third = roll1 + roll2
        print("third: " + str(third)) 
        rounds = rounds + 1

# Here I want to insert the loop that outputs the appearance of a certain value that comes from the addition of two dice rolls.-->
#####
#here is what i´ve tried so far:

        two = 0
        if third == 2:
            two = two + 1
        print(two)
            
        three = 0
        if third == 3:
            three = three + 1
        print(three)
### it only updates the variable one time, and each time from 0 again

def dice_roll():
    diceRoll = random.randint(1, 6)
    return diceRoll

main()
每次循环迭代时,变量都会返回值0,因此它无法累计总量,条件变为true

非常感谢。

试试这样:

if condition:
  myList.append(x) # or do something else

这里的
condition
是您的条件,而
myList中的
是您的列表。

据我所知,您需要计算2和3的出现次数。代码中的问题是,您在循环中定义了
2
3
,因此每次都将它们重置为零。将它们移到循环之外,它将变成:

player_input = input("how many rounds? ")

def main():
    roll1 = 0
    roll2 = 0 
    rounds = 1
    two = 0
    three = 0
    while rounds <= int(player_input):
        roll1 = dice_roll()
        roll2 = dice_roll()
        third = roll1 + roll2
        print("third: " + str(third)) 
        rounds = rounds + 1

        if third == 2:
            two += two + 1
            
        if third == 3:
            three = three + 1

def dice_roll():
    diceRoll = random.randint(1, 6)
    return diceRoll

main()
player\u input=input(“多少回合?”)
def main():
roll1=0
roll2=0
轮数=1
二=0
三=0
而下面的代码是您正在寻找的内容的“密集”版本。
代码使用defaultdict作为计算每个滚动组合出现次数的方法

import random
from collections import defaultdict

data = defaultdict(int)
player_input = input("how many rounds? ")
rounds = 0
while rounds <= int(player_input):
        roll1 = random.randint(1, 6)
        roll2 = random.randint(1, 6)
        data[roll1 + roll2] += 1
        rounds += 1
print(data)
随机导入
从集合导入defaultdict
数据=defaultdict(int)
玩家输入=输入(“多少回合?”)
轮数=0

虽然问题不清楚,但请添加您当前的代码和/或所需操作的清晰示例是的,谢谢,我编辑了我的帖子,希望现在它更清晰。谢谢,我编辑了m帖子,以便更清楚我的问题是什么。我想要一个迭代,但如果某个条件为真,则每次循环迭代时都要签入。如果条件为真,我想将某个变量或列表的值增加+1。谢谢你的密集版本。这对我来说仍然是雄心勃勃的,我正在学习对编码和python的基本理解。在这里你需要添加一个带有特定函数的库,对吗?@BenjaminDiLorenzo不需要任何外部库
defaultdict
是python核心的一部分(就像
random
)。代码使用dict来计算找到每个辊对的次数。
import random
from collections import defaultdict

data = defaultdict(int)
player_input = input("how many rounds? ")
rounds = 0
while rounds <= int(player_input):
        roll1 = random.randint(1, 6)
        roll2 = random.randint(1, 6)
        data[roll1 + roll2] += 1
        rounds += 1
print(data)