0

I'm trying to create projects with Python and today's project was a dice roller for tabletop roleplaying games like Dungeons & Dragons. I defined a function for each type of dice from D4 to D100. I thought I structured my while loop properly with nested If/Elif statements based on the user's input. However, regardless of what the user inputs, only a D20 is ever rolled? The D20 function is the first If statement in my While loop, so my guess is that something is wrong with that line in particular:

Did I somehow write this in such a way that any input will satisfy the first If statement regardless of what was input by the user, without proceeding to check the Elif statements?

the While loop:

while not done:
    diceroll = str(input("Roll the Dice! You can enter D4, D6, D8, D10, D12, D20, D100, or Custom. Q to exit:"))
    print('You entered ' + str(diceroll) + ':')
    if diceroll == 'd20' or 'D20':
        rolld20()
    elif diceroll == 'd12' or 'D12':
        rolld12()
    elif diceroll == 'd%' or 'D%' or 'd100' or 'D100':
        rolld100()
    elif diceroll == 'd10' or 'D10':
        rolld10()
    elif diceroll == 'd8' or 'D8':
        rolld8()
    elif diceroll == 'd6' or 'D6':
        rolld6()
    elif diceroll == 'd4' or 'D4':
        rolld4()
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
jacket562
  • 1
  • 1
  • To test multiple values, you could write like, `diceroll in ['d20', 'D20']`. Currently the first `if` statement will be executed regardless of the value of `diceroll`. – Zoro Apr 15 '21 at 04:08
  • @Zoro Even better, `diceroll = diceroll.lower()` or `diceroll = str(...).lower()`. – tadman Apr 15 '21 at 04:15
  • Tip: You do not need all these functions. You should have one that takes an arbitrary number and rolls that. Then you can strip off the leading `d` from your input, or even allow for things like `6d6` to be interpreted properly. Split on `d` or `[dD]` in a regular expression. – tadman Apr 15 '21 at 04:17

1 Answers1

0

what I suspect is happening is it is checking if 'D20' is always not 0 (False) you need to either standardize your input or add the other check

  • It's the `or`. That's not evaluating as expected. – tadman Apr 15 '21 at 04:16
  • Thanks!! I chopped off the Or D20 and it resolved the problem for now. I see what I did by not adding the full check on the other side of the Or statement. Rookie mistake! Thanks! – jacket562 Apr 15 '21 at 04:18