1

im currently working on a simple text adventure in python. basically i want the randint to choose 1 or 2 then assign to right_wire, then raw_input to be wire_choice, then the 2 are matched to deliver the desired outcome. Im sure you guys can figure out what im trying to do here anyway. am i close here or really way off? Thanks!

    right_wire = randint(1, 2)

    wrong_wire = 0

    if right_wire == 1:
        wrong_wire = 2
    elif right_wire == 2:
        wrong_wire = 1
    else:
        print "ERROR"

    while True:
        print "Which wire do you pull?"
        print
        print "1. The red one."
        print "2. The blue one."
        print
        wire_choice = raw_input('*>>*')

        if wire_choice == 1 and right_wire == 1:
            print "**BOMB DEFUSED**"
            return 'engineering2'
        elif wire_choice == 2 and right_wire == 2:
            print "**BOMB DEFUSED**"
            return 'engineering2'
        elif wire_choice == 1 and right_wire == 2:
            print "**FAILED**"
            return 'death'
        elif wire_choice == 2 and right_wire ==1:
            print "**FAILED**"
            return 'death'
        else:
            print no_understand
Textmode
  • 509
  • 3
  • 18
samsam
  • 25
  • 3

2 Answers2

5

Why not just :

while True:
    print "Which wire do you pull?"
    print
    print "1. The red one."
    print "2. The blue one."
    print
    raw_input('*>>*')
    if randint(0, 1):
        print "**BOMB DEFUSED**"
        return 'engineering2'
    else:
        print "**FAILED**"
        return 'death'

/!\ raw_input return a string and you're comparing the return value with an integer, it will never match :

$ python
Python 2.6.6 (r266:84292, Dec 26 2010, 22:31:48) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 == "1"
False
Julien Palard
  • 8,736
  • 2
  • 37
  • 44
  • that works absolutely perfectly, thank you very much. Its simplicity is confusing me somewhat however. how does it know to 'connect' the user input and the random number? sorry, been a long day coding! – samsam Feb 03 '13 at 17:53
  • 1
    It doesn't-- it ignores the user's input. But the user doesn't know that, since the "right" answer is chosen at random and used only once. So it's a trick, and not to be used in general... – alexis Feb 03 '13 at 17:56
4

Python is a "strongly typed" language. It doesn't turn text (a string) into a number (an int) automatically. It doesn't turn the number randint(0, 1) into a boolean either. But you can tell it to make conversions such as str(right_write) or int(wire_choice).

Converting a number to a boolean is a little different. Python objects have a method nonzero which Python calls when needed, implicitly, to determine whether the object is to be considered false or true. And booleans are subclass of int, so they have integer values. You still have the option to convert explicitly as bool(randint(0, 1)) or by using an expression like randint(0, 1) == 0.

I haven't explained exactly how to code your solution because I feel certain that you can take it from there and because you can make your own choice of how you prefer to express your code.

P.S. I'd like to link to more information about the special case of the boolean value of non-boolean objects. In particular, Python specifically permits us to test the truth value of non-booleans. And we are advised to do so, or at least, Google's guide recommends testing the truth value of non-booleans.

Community
  • 1
  • 1
minopret
  • 4,726
  • 21
  • 34
  • thank you very much for your guidance, will look into and play with this idea. – samsam Feb 03 '13 at 17:54
  • @Wooble you have a point that I have attempted to address as a "P.S." But consider that `>>> True is 1` and `>>> False is 0` both print `False`. – minopret Feb 04 '13 at 01:26
  • @Wooble thanks, my mistake, but the point I intended is that isinstance(True, bool) and not isinstance(1, bool). That distinction helps me because I wanted to focus sharply on the general, intentional lack of automatic type conversion in Python, which contrasts with (for example) Perl. I hope that clears up my interest in the distinction between True and 1. Anyway, I doubt I have anything more that is helpful to say about it. – minopret Feb 04 '13 at 16:41