2017-07-12 3 views
1

J'essaie de faire un programme de dés en python qui lance les dés en fonction de l'entrée des utilisateurs sur les côtés, les dés et les jets. Actuellement ce code fonctionne plus ou moins, mais le problème que je suis en train de dire est que je donne 3 dés étant roulés 3 fois avec 6 côtés à eux.Dice Roller pour Python

Le code Je affiche ce que la sortie:

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 

Quand je besoin pour afficher comme:

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

Voici mon code jusqu'à présent. Je suppose qu'il doit faire quelque chose avec mes arguments et paramètres étant vides? Je ne suis pas entièrement sûr. Voici mon code:

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

Répondre

2

Votre problème est que vous mettez votre déclaration print dans la boucle for intérieure, ce qui signifie que vous imprimez les résultats trop de fois, ainsi que trop souvent nrolls augmentation.

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

Pour un minimum de modifications à votre code, changer à ceci:

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

De plus, la ligne suivante renvoie une erreur.

return dice() 

Modifier à:

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

Vous devez utiliser une boucle while pour poursuivre la demande de rouler les dés, ce montant de dés.

par exemple.

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