Home > Software design >  Virtual Dice not working when asked whether to add the amount of dice values together
Virtual Dice not working when asked whether to add the amount of dice values together

Time:10-07

To check if I had learnt a tutorial properly I have been working on this 'Virtual Dice' program. First it asks how many sides the die should have, and then how many dice used. Finally, if there is multiple dice, it asks whether to add the values of the two dice together. If 'yes' is inputted, it works fine but if 'no' is, it just shows ones instead of showing multiple individual values.

import random
import time

running = True
rolling = True
yes = ("yes", 'y', 'Y', 'Yes', 'YES')
no = ("no", 'n', 'N', 'No', 'NO')

if running is True:
    print('Welcome to Virtual Dice.')
    time.sleep(0.5)
    sides = int(input('Pick the amount of sides: '))
    amount = int(input('Pick the amount of dice: '))

    if amount >= 0:
        add = input(f'Would you like to add the {amount} dice values together? ')

    if add in yes:
        total = ((1 * sides) * amount)
        lowest = (1 * amount)
    elif add in no:
        total = (1 * sides)
        lowest = 1
    else:
        print("Invalid Input")
        print("Answering with no...")
        time.sleep(1)

    no_add = str(random.randint(lowest, total)) * amount

    while rolling is True:
        print("Rolling....")
        time.sleep(1)

        if add in yes:
            print(random.randint(lowest, total))
        else:
            print(no_add)

        ask = input("Do you want to roll more? (y/n): ")
        if ask in yes:
            rolling = True
        elif ask in no:
            rolling = False
            print("Thank you for using Virtual Dice.")
            time.sleep(0.5)
            print("Ending Virtual Dice...")
            time.sleep(1)
        else:
            print("Invalid Input")
            print("Ending Virtual Dice...")
            time.sleep(1)
            rolling = False

A fix for this problem would be great.

CodePudding user response:

You only generated one random number. And displayed that three times... The following should work:

import random
import time

running = True
rolling = True
yes = ("yes", 'y', 'Y', 'Yes', 'YES')
no = ("no", 'n', 'N', 'No', 'NO')

if running is True:
    print('Welcome to Virtual Dice.')
    time.sleep(0.5)
    sides = int(input('Pick the amount of sides: '))
    amount = int(input('Pick the amount of dice: '))

    if amount >= 0:
        add = input(f'Would you like to add the {amount} dice values together? ')

    if add in yes:
        total = ((1 * sides) * amount)
        lowest = (1 * amount)
    elif add in no:
        total = (1 * sides)
        lowest = 1
    else:
        print("Invalid Input")
        print("Answering with no...")
        time.sleep(1)

    while rolling is True:
        print("Rolling....")
        time.sleep(1)

        if add in yes:
            print(random.randint(lowest, total))
        else:
            for i in range(amount):
                print(str(random.randint(lowest, total)))

        ask = input("Do you want to roll more? (y/n): ")
        if ask in yes:
            rolling = True
        elif ask in no:
            rolling = False
            print("Thank you for using Virtual Dice.")
            time.sleep(0.5)
            print("Ending Virtual Dice...")
            time.sleep(1)
        else:
            print("Invalid Input")
            print("Ending Virtual Dice...")
            time.sleep(1)
            rolling = False
  • Related