2017-07-12 4 views
1

私は、サイコロでサイコロを動かすプログラムを作ろうとしています。現在のところ、このコードは多かれ少なかれ動作しますが、私が実行している問題は、3サイコロを転がして3回、6と表示しているということです。Dice Roller for Python

私は出力として表示され、これを持っているコード:私はそれのように表示する必要がある場合には

Roll #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 

を:

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()   

答えて

2

あなたの問題は、あなたがあまりにも頻繁に使用すると、結果を印刷回数が多すぎるだけでなく、増分nrollsを意味インナーforループ、内部であなたのprint文を入れています。あなたのコードに最小限の変更については

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 
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() 

に変更し、それを:あなたはサイコロの量を転がり、その後サイコロを求めるを継続するためにwhileループを使用する必要があります

return dice 
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 
0

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")