在python中计算骰子的卷数
因此,我是一个初学者程序员,我试图计算两个骰子加起来等于11的掷骰子数。这是我尝试过的代码,但每次运行它时,它都会给我“无”输出。我做错了什么在python中计算骰子的卷数,python,Python,因此,我是一个初学者程序员,我试图计算两个骰子加起来等于11的掷骰子数。这是我尝试过的代码,但每次运行它时,它都会给我“无”输出。我做错了什么 from random import randint def die_roll1(): return(randint(1, 6)) def die_roll2(): return(randint(1,6)) def roll_sum(): sum = die_roll1() + die_roll2() count =
from random import randint
def die_roll1():
return(randint(1, 6))
def die_roll2():
return(randint(1,6))
def roll_sum():
sum = die_roll1() + die_roll2()
count = 2
if sum == 11:
return(count)
else:
count +=2
roll_sum()
print("The number of rolls are: ", roll_sum())
您没有在
else
块中返回roll\u sum()
的结果。但是,考虑使用<<代码>而<代码>循环,因为简单情况下的递归一般为非Pyththic:
import random
tries = 1
while True:
a, b = random.randint(1, 6), random.randint(1, 6)
if a + b == 11:
winning = [a, b]
break
tries += 1
输出:
22
[5, 6]
从技术上讲,只需在else部分之后返回计数即可。如果滚动两个非11,那么结果永远不会递归地传递给所有函数调用,因此它会在limbo中丢失并返回None
但是,在这种情况下,代码永远不会生成除4以外的数字,因为每次滚动11时,都会将计数变量重置为2
这可以通过使计数变量全局(函数外部)并在函数内部递增来解决。这也可以通过在while循环之外声明变量来解决。迭代程序几乎总是比递归程序可读性更好,解决这个问题的程序肯定应该是迭代的。其他答案为您提供了一个迭代解决方案。 但是,由于没有人向您确切地展示了如何更正您自己的(递归)程序,我将这样做。我认为从长远来看,你可以学到一些对你(和其他初学者)有帮助的东西 这是正确的代码
from random import randint
def die_roll():
return(randint(1, 6))
def roll_sum(count):
sum = die_roll() + die_roll()
if sum == 11:
return(count)
else:
count += 2
return roll_sum(count)
# Call roll_sum with 2 as your initial count
roll_sum(2)
解释
- 当if语句块有一个return语句时,else语句块也应该始终有一个return语句(任何elif语句块也应该有一个return语句)。如果Python找不到显式的return语句(这是原始程序中发生的情况),它将返回一个名为None的特殊对象。 因此,您需要确保通过函数的每个可能的流都会导致返回语句。在else块中的递归调用之前修复了此问题
- 在roll_sum函数中初始化count=2会导致每次(递归)调用该函数时,都将2分配给count。这显然不是你想要的。相反,您可以添加一个名为count的函数参数,这将解决此问题。 在roll_sum的初始调用中,必须传递count的初始值李>
- 不需要有两个做完全相同事情的函数——die_roll1和die_roll2。一个函数应该只封装一个功能。您可以只调用同一个函数两次。如果你想明确你有两个独立的骰子卷,你可以添加两个变量李>
祝你一切顺利 以下是几个简单的解决方案,用于计算两个骰子滚动过程中每次求和的次数。要回答您的特定问题,您只需使用“特定解决方案”代替下面的“一般解决方案”
dice1 = [1,2,3,4,5,6]
dice2 = [1,2,3,4,5,6]
totals = []
for num in dice1:
for roll in dice2:
sumz = str(num+roll)
if [sumz, 0] not in totals:
temp = [sumz, 0]
totals.append(temp)
for item in totals:
for num in dice1:
for roll in dice2:
# General Solution - sum of all roles
sumz2 = str(num+roll)
if sumz2 == item[0]:
item[1] += 1
# Specific Solution - only count number of 11's rolled
if sumz2 == '11':
if item[0] == '11':
item[1] += 1
print(totals)
# Using a dictionary to count
totals = {}
for num in dice1:
for roll in dice2:
sumz = str(num+roll)
if sumz not in totals:
totals[sumz] = 1
else:
totals[sumz] += 1
print(totals)
# using collections
from collections import Counter
# temp list to hold all sumz, including multiple entries of same value
temp = []
for num in dice1:
for roll in dice2:
temp.append(num+roll)
# unsorted dictionary of key value pairs
d = Counter(temp)
list_sorted_by_key = []
for i in sorted (d):
temp2 = (i, d[i])
list_sorted_by_key.append(temp2)
print(list_sorted_by_key)
roll\u sum
不返回任何内容(因此当sum!=11
。如果在第一次运行时,如果a+b
等于11
,则尝试将为0
。我本以为应该是1
@quamrana啊,我错过了。请看我最近的编辑。
dice1 = [1,2,3,4,5,6]
dice2 = [1,2,3,4,5,6]
totals = []
for num in dice1:
for roll in dice2:
sumz = str(num+roll)
if [sumz, 0] not in totals:
temp = [sumz, 0]
totals.append(temp)
for item in totals:
for num in dice1:
for roll in dice2:
# General Solution - sum of all roles
sumz2 = str(num+roll)
if sumz2 == item[0]:
item[1] += 1
# Specific Solution - only count number of 11's rolled
if sumz2 == '11':
if item[0] == '11':
item[1] += 1
print(totals)
# Using a dictionary to count
totals = {}
for num in dice1:
for roll in dice2:
sumz = str(num+roll)
if sumz not in totals:
totals[sumz] = 1
else:
totals[sumz] += 1
print(totals)
# using collections
from collections import Counter
# temp list to hold all sumz, including multiple entries of same value
temp = []
for num in dice1:
for roll in dice2:
temp.append(num+roll)
# unsorted dictionary of key value pairs
d = Counter(temp)
list_sorted_by_key = []
for i in sorted (d):
temp2 = (i, d[i])
list_sorted_by_key.append(temp2)
print(list_sorted_by_key)