代码之家  ›  专栏  ›  技术社区  ›  Anacletoxx

Python掷骰子有2个参数:骰子的边数和骰子数

  •  0
  • Anacletoxx  · 技术社区  · 8 年前
    1. 这就是我遇到的问题:编写一个函数roll dice,它包含两个参数——骰子的边数和骰子数 就这些!示例输出

         >>>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)
      
    2 回复  |  直到 8 年前
        1
  •  1
  •   gommb    8 年前

    试试这个:

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

    这使用了 for loop 到循环 rolls 并打印一个介于1和之间的随机数 sides 每个回路。

        2
  •  0
  •   Styx    8 年前
    import random
    def roll_dice(attempts,number_of_sides):
      for i in range(attempts):
         print(random.randint(1, number_of_sides))
      print("thats all")
    
    推荐文章