Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I need help formating an output

Im making an ‘interface’ for a war game with my freinds so we dont need 100’s of cards they can all be in a program. in also adding a dice roller to it i found an article that worked but was a bit outdatted really inefficent and didnt work with my program for a couple of reasons so i decided to rewrite it my self and it worked but the only thing i strugled with was the format of the output at the minite mine looks like this

1
3
2
4
5
4

and the article i founds looked like this (after some slight modification)

[1,3,2,4,5,4]

and im not sure how to get it like this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import random

def dice_roller():
    num = input('Enter a number from [1-20] or "home" to exit')
    if num in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']:
        for i in range(int(num)):
            print(random.randint(1,6))

this is the code its not done yet any ideas Thanks.

>Solution :

You can use list comprehensions to get your output in a list

import random

 
def dice_roller():
    num = input('Enter a number from [1-20] or "home" to exit')
    if num in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20']:
        res_list = [random.randint(1,6) for i in range(int(num))]
    print(res_list)
    
print(dice_roller())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading