0

so i have been working on a input based dungeon game in python that is fairly rudimentary. I wanted to put a dice roll condition in the game and decide if a player will die or live on a path. However my if statement will not properly respond to the roll in the number generator i have in the program, it will print the number and then carry on whether or not the condition was met. How can i fix this, and why is this happening?

if direction == 1:
    import random
    from random import *
num = 6

def d6(num):
rolls = []
for x in range(num):
    rolls.append(randint(1,6))
return rolls

rolls = d6(1)
print rolls

if rolls is 3:
    print "you fall in a hole!\n"
    print ' OH DEAR YOU ARE DEAD!'
    raise SystemExit
elif rolls is not 3:
    print "you are back at the start which way will you go?\n"

path3 =float(input("forward, or right?"))
noige
  • 11
  • You printed `rolls`, so I'm sure you've seen what it is. Why do you think it's not only going to be equal to `3`, but _have the same identity_ as `3`? – TigerhawkT3 May 05 '16 at 03:58
  • A few related posts: http://stackoverflow.com/questions/36898917/in-python-when-are-two-objects-the-same/36899294 , http://stackoverflow.com/questions/14247373/python-none-comparison-should-i-use-is-or/14247383#14247383 , http://stackoverflow.com/questions/14247373/python-none-comparison-should-i-use-is-or/14247383#14247383 – mgilson May 05 '16 at 04:00
  • Why are you conditionally importing `random`? `rolls` looks like a list, what do you expect to get out of `if a_list is 3`? Finally, it'd probably be helpful if you cleaned up the indentation. It looks mostly OK except for the stuff in `d6`. Is that correct? – mgilson May 05 '16 at 04:02

1 Answers1

-1

is and is not are reserved for special comparisons in python. You need == and !=. Also you can change it to a simple if/else instead of if/elif with no else. Also, it looks like your d6 function returns a list instead of a single number. Try adjusting that to return a single number instead:

from random import randint

def d6():
    return randint(1,6)

rolls = d6()
if rolls == 3:
    print "you fall in a hole!\n"
    print ' OH DEAR YOU ARE DEAD!'
    raise SystemExit
else:
    print "you are back at the start which way will you go?\n"
tknickman
  • 4,285
  • 3
  • 34
  • 47