-2

I have created a text based adventure in Python using functions and random integers. My second random integer will not work. When I run the code through the first random integer, it works fine. But the second random integer does not work, even though it is the same code as the first random integer, just different variables. I'm not sure what the problem is, and i'm looking for help.

def pathChoice():
    path=""
    while path != "1" and path != "2":
        path=input("What will Levi do? (1 or 2): ")
    return pathChoice

def checkPath(pathChoice):
    print(".....................................................................")
    print("Levi sneaks around the side of the castle, avoiding suspecious guards.")
    time.sleep(3)
    print("Levi notices a small window that he could climb into.")
    time.sleep(2)
    print("He must scale the wet stone wall without falling.")
    time.sleep(2)

    correctMovement = random.randint(1,2)

    if pathChoice == str(correctMovement):
        print("...............................................................")
        print("He scales the wall carefully, avoiding the loose rubble and climbs into the castle")
        time.sleep(2)
        print("Levi sneaks behind some barrels and boxes, making his way past some guards.")
        time.sleep(2)

    else:
        print(".................................................................")
        print("The wet stone wall is too slipery, and Levi falls half way up.")
        time.sleep(2)
        print("The loud thud of his corpse alerts the nearby guards.")
        time.sleep(2)
        print("Levi is apprehended by the guards.")

def castleScene():
    print("....................................................................")
    print("Levi is dragged to a jail cell by two guards after trying to sneak into the castle.")
    time.sleep(2)
    print("He feels tired and discouraged as the guards close and lock the door.")

def secondPathChoice():
    pat=""
    while pat !="3" and pat !="4":
        path=input("What does Levi do? (3 or 4)")
    return secondPathChoice

def InsideCastlePath(secondPathChoice):
    print("...................................................................")
    print("Levi is put into a jail cell.")
    time.sleep(2)
    print("He notices some loose wall shards laying on the ground.")
    time.sleep(2)
    print("Levi picks up the shards and wonders if he can sharpen one of them to make a lockpick.")
    time.sleep(2)
    print("Through the night he works tirelessly to make a lockpick, and by morning he successfully makes one.")
    time.sleep(2)
    print("Levi must escape the jail cell when the guards are not paying attention.")

    successfulLockpick = random.randint(3,4)

    if secondPathChoice == str(successfulLockpick):
        print(".................................................................")
        print("Levi successfully lockpicks the jail cell door and makes his way to the kings treasure room.")
        time.sleep(2)
        print("")

    else:
        print("..................................................................")
        print("The lockpick breaks off in the jail cell door and Levi never escapes.")


Restart="yes"
while Restart == "yes" or Restart =="y":
    intro()
    choice = pathChoice()
    checkPath(choice)
    castleScene()
    choice2 = secondPathChoice()
    InsideCastlePath(choice2)
    Restart=input("Would you like to Restart Levi's Adventure? (yes or y to `restart): ")
  • 3
    what is the 'first' and 'second' random integers? – depperm Nov 28 '18 at 15:52
  • unrelated: `while pat !="3" and pat !="4":` ==> `while pat not in ("3","4"):` .... `print(".................................................................")` ==> `print("." * 66)` – Patrick Artner Nov 28 '18 at 15:54
  • correctMovement and successfulLockpick – Nathan Long Nov 28 '18 at 15:54
  • 1
    Define "does not work" and specify which line is failing, what you expect to happen, and what is actually happening. – Larry Lustig Nov 28 '18 at 15:56
  • successfulLockpick = random.randint(3,4) is not running. The input for secondPathChoice will run, but 3 or 4 cannot be entered to run if secondPathChoice == str(successfulLocpick): – Nathan Long Nov 28 '18 at 16:01

2 Answers2

2

You got a typo:

def secondPathChoice():
    pat=""
    while pat !="3" and pat !="4":
        path = input("What does Levi do? (3 or 4)")
    return secondPathChoice

It is pat not path => infinite loop


You should bundle your functions a bit, following DRY: don't repeat yourself

def get_input(text, options):
    pat = ""
    while pat not in options:
        pat = input(text)
    return pat

and use it like so:

answer1 = get_input("What will Levi do? (1 or 2): ", {"1","2"})
answer2 = get_input("What does Levi do? (3 or 4): ", {"3","4"})

That way you can re-use the logic of asking user for input until valid with different texts and options without re-coding the method all over again.

See also: Asking the user for input until they give a valid response

Also minor tips:

while pat !="3" and pat !="4": 

is more consicely written as

while pat not in {"3","4"}:

If you need to print multiples of the same char use print("." * 66) to print 66 dots.


I do not quite get why you use the randomization - the player chooses one option and you vary whats correct. That is useful so they can not memorize the 'correct' solution .. but ... its the same as when you never ask the user at all and simply just choose one random path ...

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

welcome to the fabulous world of programming ;)

Unlike real life, spelling is extremely important here. A single character can make or break your program, as is the case here:

def secondPathChoice():
    pat=""
    while pat !="3" and pat !="4":
        path=input("What does Levi do? (3 or 4)")
    return secondPathChoice

pat is not path and is not secondPathChoice.

Good luck!

smassey
  • 5,875
  • 24
  • 37
  • 2
    I don't like the implication that spelling is not important in real life. But I guess that's where I am at odds with real life. =| – khelwood Nov 28 '18 at 16:10
  • Spelling in real live is important - there's a whole culture of people earning money with spelling mistakes and by interpreting weird stuff into what's not that clearly written: _lawyers_ – Patrick Artner Nov 28 '18 at 16:13
  • Of course it has some importance, but it's not *as* important! Most.. humans can/will read over your typos without even noticing, maybe they'll notice but just shrug it off and read on, but your compiler/interpreter won't be as lax. That said, I look forward to future interpreters that understand my *obvious* typos and just chug along :) – smassey Nov 28 '18 at 16:24