0

I'm trying to write a dice roller as practice. I've tried to look into try, except, while, but neither works as I intend it - before asking if the user wants to continue, I'd want to check if the user input is valid, and if not, return to user_number1. Am I looking at this from the wrong angle? What can I do with this? Sorry for the possibly stupid question, I'm pretty new to this.

import random
print("Welcome to the dice roller!")

def roller():
    user_number1 = input("Please input the dice you want to use in the following format: XdY > ")
    user_number_fin = user_number1.split("d")
    num1 = int(user_number_fin[0])
    num2 = int(user_number_fin[1])

    if num1 == 1:
        result1 = random.randint(num1, num1*num2)
        print("Your roll is: " + str(result1) + " (" + str(num1) + "d" + str(num2) + ")" )

    else:
        dice_number = 1
        list_of_results = []
        while dice_number <= num1:
            result2 = random.randint(num1, num2)
            list_of_results.append(result2)
            dice_number += 1
        print("Your roll is: " + str(sum(list_of_results)) + " (" + str(num1) + "d" + str(num2) + ", " + str(list_of_results)+ ")")


def shouldi():
    roller()
    usercont = input("Do you want to continue? y/n > ")
    while usercont in ["Y", "y"]:
        roller()
        usercont = input("Do you want to continue? y/n > ")

    if usercont in ["N", "n"]:
        print("Thank you for using the dice roller. Bye!")
        quit()
    else:
        print("That is not a valid input.")
        usercont
qgloaf
  • 11
  • 2
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Patrick Haugh Jul 13 '18 at 18:04
  • You may want to use regular expressions to check the validity of the input eg. `user_number_fin = re.match("(\\d?)d(\\d?)",user_number1,re.I)` then you can do a loop: `while not user_number_fin: read again` – Onyambu Jul 13 '18 at 19:15

2 Answers2

0

Something like below is an alternate approach to using regex. If you're comfortable with regex then I would prefer using that instead of this. This is only an alternate approach.

def roller():
user_number1 = input("Please input the dice you want to use in the following format: XdY > ")
if("d" in user_number1):
    if(len(user_number1) == 3):
        user_number_fin = user_number1.split("d")
        num1 = int(user_number_fin[0])
        num2 = int(user_number_fin[1])
 else:
     print("Your input not in valid format. Use the format XdX")    
Syed
  • 76
  • 7
0

You can use regular expressions and write a function that does exactly what you need then you can use it within your roller() function:

import re

def get_number():
    user_number1 = input("Please input the dice you want to use in the following format: XdY > ")
    user_number_fin = re.match("^(\\d*)d(\\d+)$",user_number1,re.I)
    if not user_number_fin: get_number()
    if user_number_fin.group(1) =='': num1 = 1
    else: num1 = int(user_number_fin.group(1)) 
    num2 = int(user_number_fin.group(2)) 
    if num1>num2:
        print("\n\tSorry--side to roll must be less than the number of sides!!")
        get_number()
    return {'num1':num1,'num2':num2}

This can accept d4 ie taking the default side if not given to be 1, and cannot accept 4d3 ie the side to be rolled must be less than the number of sides present in the dice.

Onyambu
  • 67,392
  • 3
  • 24
  • 53