尝试用Python构建一个用户输入掷骰子程序,但我可以';无法将我的输入输入到def中

尝试用Python构建一个用户输入掷骰子程序,但我可以';无法将我的输入输入到def中,python,dice,Python,Dice,这是到目前为止的代码。我不熟悉Python和编程,所以这有点像是一种编码。 我想让用户输入他们想要滚动的模具尺寸以及他们想要滚动的数量。一旦我把这个编码好,我就什么也得不到了 import random die_size = int(input('What is the size of your die? [enter a #]')) rolls = int(input('How many would you like to roll? [enter a #]')) def dice_sim

这是到目前为止的代码。我不熟悉Python和编程,所以这有点像是一种编码。 我想让用户输入他们想要滚动的模具尺寸以及他们想要滚动的数量。一旦我把这个编码好,我就什么也得不到了

import random

die_size = int(input('What is the size of your die? [enter a #]'))
rolls = int(input('How many would you like to roll? [enter a #]'))

def dice_sim(die_size, rolls):

results = 0
dice_sum = 0

    for i in range(0,rolls):
        results = random.randint(1,die_size)
        print("Die %d rolled %d." % (i+1,results))
        dice_sum += results

    print("Total of %d dice rolls is: %d" % (rolls,dice_sum))

    return None

除了一些语法错误外,还需要调用函数,将所需的输入作为参数传递,并返回计算结果

在修复语法并根据需要调用函数后,您的代码应该如下所示:

import random

# fixed errors (indentation and function name)
def dice_sum(die_size, rolls):
    results = 0
    dice_sum = 0    
    for i in range(0, rolls):
        results = random.randint(1, die_size)
        print("Die %d rolled %d." % (i+1, results))
        dice_sum += results
    print("Total of %d dice rolls is: %d" % (rolls, dice_sum))
    # return the result of your calculation instead of `None`
    return dice_sum

# get user input (you might think about catching errors)
die_size = int(input('What is the size of your die? [enter a #]'))
rolls = int(input('How many would you like to roll? [enter a #]'))

# pass input values to function and print result
result = dice_sum(die_size=die_size, rolls=rolls)
print(result)

代码甚至不编译,因为它是这样缩进的,与您的代码是一样的吗?因为
def
后面至少有一行应该缩进,或者函数没有正文。另外,您是否真的在任何时候调用了
dice\u sim
?实际上,行
results=0
dice\u sum=0
的缩进似乎已关闭。应将它们缩进四个空格,以便在
def dice\u sim