骰子辊为Python

时间:2017-07-12 23:31:17

标签: python nested-loops dice

我试图在python中制作一个骰子滚动程序,根据用户输入的侧面,骰子和掷骰子掷骰子。目前这个代码或多或少都有效,但我遇到的问题是我将3个骰子36Roll #1 6 Roll #2 5 Roll #3 1 Roll #4 6 Roll #5 4 Roll #6 6 Roll #7 3 Roll #8 1 Roll #9 1 次。{/ p>

我的代码将其显示为输出:

Roll #1 6 5 1
Roll #2 6 4 6
Roll #3 3 1 1

当我需要它显示为:

import random

def main ():
    rolls = get_rolls()
    dice = get_dice()
    sides = get_sides()

    nrolls = 1
    for r in range (rolls):
        for d in range (dice):
            print ('Roll #', nrolls, random.randint(1,sides))
            nrolls += 1 

def get_rolls():
    rolls = int(input('Enter the number of rolls: '))
    while rolls <= 0:
        print ('Number of rolls must be higher than 0')
        rolls = int (input('Enter the number of rolls: '))
    return rolls

def get_dice():
    dice = int (input('Enter the number of dice being rolled: '))
    while dice < 1 or 5 < dice:
        print ('Number of dice being rolled must be between 1 and 5')
        dice = int (input('Enter the number of dice being rolled: '))
    return dice ()

def get_sides():
    sides = int (input('Enter the number of sides on the dice: '))
    while sides < 2 or 36 < sides:
        print ('Number of sides on dice must be between 2 and 36')
        sides = int (input('Enter the number of sides on the dice: '))
    return sides


main()        

到目前为止,这是我的代码。我的猜测是它必须做我的参数和参数为空的东西?我不完全确定。这是我的代码:

{ homeSliderImage: '/assets/images/uploads/image-homeSliderImage.jpg' }

4 个答案:

答案 0 :(得分:2)

您的问题是您将print语句放在内部for循环中,这意味着您打印结果的次数太多,并且过于频繁地增加nrolls

nrolls = 1
for r in range (rolls):
    roll = []
    for d in range (dice):
        roll.append(random.randint(1,sides))
    print('Roll #', nrolls, *roll)
    nrolls += 1
>Roll # 1 3 3 1
>Roll # 2 1 5 2
>Roll # 3 6 5 4

答案 1 :(得分:0)

要对代码进行最少的修改,请将其更改为:

for r in range (rolls):
    print('Roll #', nrolls, end=' ')
    for d in range (dice):
        print (random.randint(1,sides), end=' ')
    print()
    nrolls += 1

此外,以下行会引发错误。

return dice ()

将其更改为:

return dice

答案 2 :(得分:0)

import random

def dice_roll(n):
dicelist = ['\u2680','\u2681','\u2682','\u2683','\u2684','\u2685']
dicevalue = {(dicelist[0]): 1, (dicelist[1]): 2, (dicelist[2]): 3, 
(dicelist[3]): 4, (dicelist[4]): 5,(dicelist[5]): 6}
result = 0
visualresult = " "

for i in range(n): 
    random_roll = random.sample((dicelist), 1)
    str1 = str(random_roll)
    str2 = str1.replace("'"," ")
    str3 = str2.replace("[", " ")
    str4 = str3.replace(']',' ')
    str5 = str4.strip()
    valueresultindex = dicelist.index(str5)
    valuelist = list(dicevalue.values())
    newvalueindex = valuelist[valueresultindex]
    result = result + newvalueindex
    visualresult = visualresult + str4
    i = i + 1

rolledstring = "You rolled this much: "    
print(visualresult)
print((rolledstring) + str(result))

Numofdice = int(input("Please enter the amount of dice you would like to 
roll: "))

if Numofdice >= 1 and Numofdice <= 6:
    ValidRollAmount = True
else:
    ValidRollAmount = False

while ValidRollAmount == False:
    Numofdice = int(input("Invalid amount of dice please re-enter: "))    
    if Numofdice >= 1 and Numofdice <= 6:
        ValidRollAmount = True

while ValidRollAmount == True:
    dice_roll(Numofdice)
    ValidRollAmount = False

答案 3 :(得分:0)

您应该使用while循环继续请求掷骰子然后滚动骰子数量。

e.g。

import random
from random import randint
import string

while True:
  str = input("What dice would you like to roll")
  prefix, d, suffix = str.partition("d")
  if str == "quit" or str == "q":
    break
  elif str.startswith("d"):
    print random.randint(1, int(suffix))
  elif str.startswith(prefix):
    for _ in range(int(prefix)):
      print (random.randint(1, int(suffix)))
  elif str.startswith("d"):
    print random.randint(1, suffix)
  else:
   print ("Keep rolling or type q or quit")

print ("exiting")