Welcome, guest | Sign In | My Account | Store | Cart

Choose a die and the program will give you the answer.

Python, 15 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
while again:
    print" Pick a dice: "
    print"4, 6, 12"
    userinput = input("Pick a dice: ")
    if userinput == 4: 
        randomnumber = random.randint(1,4)
        print randomnumber

    if userinput == 6:
        randomnumber = random.randint(1,6)
        print randomnumber

    if userinput == 12:
        randomnumber = random.randint(1,12)
        print randomnumber
   

1 comment

Michael Shepanski 10 years, 9 months ago  # | flag

Pretty good. However you have the max number in userinput. No need for the if statements. You can also put the dice choices into a list to allow yourself to easily update it in the future.

DICE = ['4','6','12']

while True:
    number = raw_input("Pick a dice [%s]: " % ", ".join(DICE))
    if number in DICE:
        print random.randint(1, int(number))
Created by superducktoxic on Thu, 11 Jul 2013 (MIT)
Python recipes (4591)
superducktoxic's recipes (5)

Required Modules

  • (none specified)

Other Information and Tasks