Python游戏:掷骰子

时间:2017-10-14 16:35:08

标签: python-3.x

def c():
dice = r.randint(1,6)
dice2 = r.randint(1,6)
craps = 2, 3, 12
natural = 7, 11
count = 0
return (dice,dice2,craps,natural,count)

for i in range(1,10000+1):
    result = c()
    dice = result[0]
    dice2 = result[1]
    craps = result[2]
    natural = result[3]
    count = result[4]
    while (dice+dice2)!= natural or (dice+dice2) != craps:
            c()
            count+=1
print("You won", count,"games!")

我希望它玩游戏掷骰子10,000次并打印赢得的游戏,但我所做的一切都以骰子或计数未定义为止,我已经尝试了几个小时,所以我想我会要求一些在这里帮忙。

编辑:我修复了返回和功能,但现在它根本不会打印任何内容。建议?已编辑代码以显示我更改的内容

1 个答案:

答案 0 :(得分:0)

def c():
dice = r.randint(1,6)  # this is limited to c() function
dice2 = r.randint(1,6) # this is limited to c() function
craps = 2, 3, 12       # this is limited to c() function
natural = 7, 11        # this is limited to c() function
return dice            # this value will return
return dice2           # this value is skipped
return craps           # this value is skipped
return natural         # this value is skipped

for i in range(1,10000+1):
    while (dice+dice2)!= natural or (dice+dice2) != craps:
            count += 1
            c()
print("You won", count,"games!")

值dice1,dice2,natural和craps在for循环中不起作用,因为它们的范围仅限于c()函数。

您可以使用tulp

返回所有值
def c():
    dice = r.randint(1,6)  
    dice2 = r.randint(1,6) 
    craps = 2, 3, 12       
    natural = 7, 11        
    return (dice,dice2,craps,natural)

获得你必须要做的价值。

for i in range(1,10000+1):
    result = c()
    dice = result[0]
    dice2 = result[1]
    craps = result[2]
    natural = result[3]
...