Im a college student this is my first computer science class and im having trouble with my code. it will not display the print statment. This is my code thus far.

import random
choice = raw_input("what die would you like to use: ")
d4 = random.randrange(4)+1
d6 = random.randrange(6)+1
d10 = random.randrange(10)+1
d12 = random.randrange(12)+1
d20 = random.randrange(20)+1
d100 = random.randrange(100)+1
if choice == d4:
print "d4"
if choice == d6:
print "d6"
if choice == 10:
print "d10"
if choice == d12:
print "d12"
if choice == d20:
print "d20"
if choice == d100:
print "d100"

And this is what i get back
what die would you like to use: d100
>>>
how do i get it to print line 3-8 depending on the user input.

I would suggest a print statement or two so you can tell what the program is doing. For future postings, we have no idea what "choice" is supposed to contain.

import random
choice = raw_input("what die would you like to use: ")
d4 = random.randrange(4)+1
d6 = random.randrange(6)+1
d10 = random.randrange(10)+1
d12 = random.randrange(12)+1
d20 = random.randrange(20)+1
d100 = random.randrange(100)+1

print "comparing choice =", choice, "and d4 =", d4
if choice == d4:
    print "d4"

print "comparing choice =", choice, "and d6 =", d6
if choice == d6:
    print "d6"

Edited 6 Years Ago by woooee: n/a

You could use something like:

import random

valid_range= (4,6,10,12,20,100)
choice = 0

while not 0 < choice <= len(valid_range):
    try:
        choice = int(raw_input("what die would you like to use: "+''.join("\n\t%i) upto %i" % (ind+1,top)
                                                                  for ind, top in enumerate(valid_range)) + '\n\t'))
        if not 0 < choice <= len(valid_range):
            raise ValueError
        
    except ValueError:
        print 'Value must be between 1 and %i' % len(valid_range)

top = valid_range[choice-1]

for count in range(12):
    d=random.randint(1, top)
    print d

I think you have your ""'s backwards on your if statement and print statement

I'll walk you through d4 for example
so the program asks the user what die they want - lets say they type "d4"
then the program randomly generates a value for the variable d4 you have - lets say it generates 3
now the if statement checks to see if choice which is a string of "d4" is equal to the integer stored in d4 which is 3

so it will not go into the if statement because "d4" != 3

what I think you want is to just reverse the quotes on your print and if statement for the d4 and all the other dx's

so that it is like this

if choice == "d4":
     print d4

this will check to see if they typed in "d4" and will print the random value that you generated

this will check to see if they typed in "d4"

This assumes that the user entered "d4" We do not know this. It is possibly more likely that just a "4" was entered as the "d" is not necessary. If a "4" was entered then the code would be
rand_d = random.randrange(int(choice))+1
So on the one hand we have a leading "d" probably not entered, and on the other we have 6 random statements, where one would do if a number was entered. We'll have to wait for the OP to reply and see.

Edited 6 Years Ago by woooee: n/a

And whatever the case, you want to verify that the correct data was entered.

if choice not in ["4", "6", "10", "12", "20", "100]:
    print "Incorrect number entered"

Also, place this under a while() loop, so it will loop back if an incorrect number was entered.

Ya but if you look at what the OP posted as input

And this is what I get back
what die would you like to use: d100

he uses the d, but if he wanted he could rip the number off the end of it and then do what you suggested which is probably an upgrade to what he has. But I think it is a beginner programming class and they might not have learned all that stuff yet.

This article has been dead for over six months. Start a new discussion instead.