Python 掷骰子有两个参数:骰子的面数和骰子的个数

Python Roll dice with 2 parameters: Number of sides of the dice and the number of dice

  1. 这是我遇到的问题:写一个函数 roll dice 接受 2 个参数——骰子的面数和骰子的数量 滚动 - 并为每个滚动的骰子生成随机滚动值。打印出每个卷,然后 return 字符串 “就这样!”示例输出

       >>>roll_dice(6,3)
       4
       1
       6 
       That's all!
    
  2. 这是我目前使用普通掷骰子代码得到的代码:

                      import random
    
                      min = 1
                      max = 6
    
                      roll_dice = "yes"
                      while roll_dice == "yes":
                          print random.randint(min,max)
                          print random.randint(min,max)
                          print "That's all"
    
                          import sys
                          sys.exit(0)
    

试试这个:

def roll_dice(sides, rolls):
    for _ in range(rolls):
        print random.randint(1, sides)
    print 'That\s all'

这使用 for loop 循环 rolls 次,并在每个循环中打印一个介于 1 和 sides 之间的随机数。

import random
def roll_dice(attempts,number_of_sides):
  for i in range(attempts):
     print(random.randint(1, number_of_sides))
  print("thats all")