Python 另一个函数中的操作函数

Python 另一个函数中的操作函数,python,function,Python,Function,我正在尝试基于random.sample([0,1],1)操作50/50玩家赢/输游戏 我的代码怎么了?random。sample将返回一个示例列表,因此test01==1总是False。 例如: 一种解决方案是test01[0]==1 或者,可以使用random.choices,一次生成所有翻转: In [188]: wins = 0 ...: losses = 0 ...: for flip in random.choices([0, 1], k=20): ..

我正在尝试基于random.sample([0,1],1)操作50/50玩家赢/输游戏


我的代码怎么了?

random。sample
将返回一个示例列表,因此
test01==1
总是
False
。 例如:

一种解决方案是
test01[0]==1

或者,可以使用
random.choices
,一次生成所有翻转:

In [188]: wins = 0
     ...: losses = 0
     ...: for flip in random.choices([0, 1], k=20):
     ...:     if flip:
     ...:         wins += 1
     ...:     else:
     ...:         losses += 1

In [189]: print(wins, losses)
7 13

正如@salt die所指出的,
random.sample()
设计用于返回总体样本,并作为列表返回

为了达到预期的结果,您可以用
random.choice()
替换
random.sample()
,这将从总体中返回单个选择:

def gameplay():
    i = 0
    while  i <= 300:
        test01 = random.choice([0,1])     # use random.choice()
        i += 1
        if test01 == 1:
            player_win()
        else:
            player_lose()
    print("player_win",player_win_count)
    print("dealer_win",dealer_win_count)
最后,就风格而言: 有时,简单地测试真实性/错误性被认为是python,因此,如果需要,可以对该代码进行一些改进:

while  i <= 300:
        i += 1
        if random.choice([0, 1]):   # A `1` is considered equivalent to True
                                    # in Python, so we can skip setting the value
                                    # of test01 to being either 1 OR 0 and
                                    # simply return a 1 or 0 into the if statement.

而我Python中的最佳实践是避免使用
全局
作用域,所以如果您真的必须使用函数,这样做会更好

随机导入
def_计数器(计数:整数):
计数+=1
返回计数
def gameplay():
平局游戏计数=0
经销商数量=0
玩家\u赢\u计数=0
对于范围(300)内的i:
test01=random.randint(0,2)
如果test01==0:
抽签游戏计数=\计数器(抽签游戏计数)
elif test01==1:
玩家获胜计数=\计数器(玩家获胜计数)
其他:
经销商赢得数量=\计数器(经销商赢得数量)
打印(“抽签”、抽签、游戏、计数)
打印(“玩家赢”,玩家赢)
打印(“经销商赢”,经销商赢计数)
如果名称=“\uuuuu main\uuuuuuuu”:
游戏性()
否则,您可以简单地执行以下操作:

随机导入
def gameplay():
平局游戏计数=0
经销商数量=0
玩家\u赢\u计数=0
对于范围(300)内的i:
test01=random.randint(0,2)
如果test01==0:
平局游戏计数+=1
elif test01==1:
玩家赢数+=1
其他:
经销商赢得数量+=1
打印(“抽签”、抽签、游戏、计数)
打印(“玩家赢”,玩家赢)
打印(“经销商赢”,经销商赢计数)
如果名称=“\uuuuu main\uuuuuuuu”:
游戏性()

你的意思是“随机选择([0,1])?这些全局变量真的是必要的吗?特别是因为你也在返回值。。。
In [188]: wins = 0
     ...: losses = 0
     ...: for flip in random.choices([0, 1], k=20):
     ...:     if flip:
     ...:         wins += 1
     ...:     else:
     ...:         losses += 1

In [189]: print(wins, losses)
7 13
def gameplay():
    i = 0
    while  i <= 300:
        test01 = random.choice([0,1])     # use random.choice()
        i += 1
        if test01 == 1:
            player_win()
        else:
            player_lose()
    print("player_win",player_win_count)
    print("dealer_win",dealer_win_count)
while  i <= 300:
        test01 = random.sample([0,1], 1)[0]    # use list indexing to get 
                                               # the zeroeth item from the list
while  i <= 300:
        i += 1
        if random.choice([0, 1]):   # A `1` is considered equivalent to True
                                    # in Python, so we can skip setting the value
                                    # of test01 to being either 1 OR 0 and
                                    # simply return a 1 or 0 into the if statement.