Home > Enterprise >  Print dice rolls on a single line
Print dice rolls on a single line

Time:01-18

import random

while True:   
    num_side = int(input("Which type of dice do You wish to roll? Select from: [4, 6, 8, 10, 12, 20, 100]: "))
    num_dice = int(input("How many dice do You wish to roll?: "))
    rolls = []
    for die in range(num_dice):
        dice_roll = random.randint(1, num_side)
        rolls.append(dice_roll)
        print ("You rolled: {}".format(rolls))

When the num_dice input == anything greater than 1 the output is printed over multiple lines, for instance if num_dice == 4 the output would look like

You rolled: [x]

You rolled: [x,x]

You rolled: [x,x,x]

You rolled: [x,x,x,x]

How could I fix this to print the rolls on a single line?

CodePudding user response:

Move the print() statement over one indent, and the results will only appear once all dice have been rolled.

import random    
while True:   
    num_side = int(input("Which type of dice do You wish to roll? Select from: [4, 6, 8, 10, 12, 20, 100]: "))
    num_dice = int(input("How many dice do You wish to roll?: "))
    rolls = []
    for die in range(num_dice):
        dice_roll = random.randint(1, num_side)
        rolls.append(dice_roll)
    print("You rolled: {}".format(rolls))

CodePudding user response:

Replace the print statement as follows

        print ("You rolled: {}".format(rolls),end=" ")

CodePudding user response:

just step your print() function out of your for loop like next


import random

while True:   
    num_side = int(input("Which type of dice do You wish to roll? Select from: [4, 6, 8, 10, 12, 20, 100]: "))
    num_dice = int(input("How many dice do You wish to roll?: "))
    rolls = []
    for die in range(num_dice):
        dice_roll = random.randint(1, num_side)
        rolls.append(dice_roll)
        
    print ("You rolled: {}".format(rolls))

  •  Tags:  
  • Related