Dice rolling simulator, any ideas on implementing any features? This is one of my mini project of dice rolling and I want to improve it further with more advanced Python. Maybe you have any ideas to improve the code itself?

This code will ask the minimum and maximum number for the dice from the user and the number of times it will roll them, giving random numbers.

import random
#Size choosing for dice
while True:
    #Choosing the minimum number
    Min = input("Please input the minimum number of your dice: ")
    try:
        Min = int(Min)
    except ValueError:
        print("Invalid input")
        continue
    #Choosing the maximum number
    Max = input("Please input the maximum number of your dice: ")
    try:
        Max = int(Max)
    except ValueError:
        print("Invalid input")
        continue
    #Check if the minimum and maximum is valid
    if Min &gt; Max:
        print("Minimum is bigger than Maximum.\n Please reinput data...")
        continue
    elif Min == Max:
        print("Minimum is equaled to Maximum,\n Please reinput data...")
        continue
    print(f"Your dice is from {Min} to {Max}.")
    break

#Random number generater
while True:
    Rollcount = input("How many times do you want to roll the dice: ")
    try:
        Rollcount = int(Rollcount)
        break
    except ValueError:
        print("Invalid input")
        continue
for i in range(0,Rollcount):
    roll = random.randint(Min,Max)
    print(roll)
I normally would agree with Graipher here, however with your code I think there's a simpler and more readable solution.


The first number doesn't have to be larger than the second.

This is an artificial limitation that can be fixed by sorting the inputted values.
Not allowing the same value to be selected is an artificial limitation. random.randint includes both bounds, meaning random.randint(n, n) is valid input.
You can bundle the first two try statements together to simplify the code.

This makes the code DRY as the two loops are logically doing two different things.


Whilst longer it keeps the functionality to change the lower bound if you enter it incorrectly, without having to kill the process.

import random

while True:
    try:
        min_, max_ = sorted([
            int(input("Please enter one of the bounds: "))
            int(input("Please enter the other bound: "))
        ])
        break
    except ValueError:
        print("Invalid input")

while True:
    try:
        amount = int(input("How many times do you want to roll the dice: "))
        break
    except ValueError:
        print("Invalid input")

for _ in range(amount):
    print(random.randint(min_, max_))


Note: You can move both the while try loops into a function, and pass a lambda, however I don't think that's as readable.

min_, max_ = while_try(lambda: sorted([
    int(input("Please enter one of the bounds: "))
    int(input("Please enter the other bound: "))
]))