Question

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
Was it helpful?

Solution 2

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.

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top