Program stopping

Question: I recently started working on a Text based RPG game, and have almost finished. But while playtesting I came across an error where it stops around halfway through without an exit() function.

Repl link: https://replit.com/@LeviRobertson1/TxtbasedRPGtests#main.py

import random
import time
import replit
from colorama import Back, Fore, Style, init
init()
def txt(sentence):
  for char in sentence:
    print(char, end='', flush=True)
    time.sleep(0.05)
  print()
def txt1(sentence):
  for char in sentence:
    print(char, end='', flush=True)
    time.sleep(0.5)
  print()
def askgrab(item):
  txt(f'Do you want to grab the {item}?')
  qna = str(input(Fore.BLUE+'y/n\n'))
  print(Style.RESET_ALL)
  if qna == 'y':
    inventory.append(currentitems[0])
    txt(f'You have grabbed the {item}')
  else:
    txt('You decide to not take the item. There was no other opportunity to get out...')
    txt('You rot to death in your cell...')
    txt('The End')
    txt(Fore.RED+'[Ending 1 of 3: The Coward]')
    exit()
def unlock(locknum):
  txt('You try to pick the lock')
  txt('Guess the number from 1 to 100 to unlock')
  guess = int(input('> '))
  if guess == locknum:
    txt('You unlocked the door')
  elif guess > locknum:
    txt('Too high, try again!')
    replit.clear()
    unlock(locknum)
  elif guess < locknum:
    txt('Too low, try again!')
    replit.clear()
    unlock(locknum)
def attack():
  txt('Do you want to attack?')
  atkcheck = str(input(Fore.BLUE+'y/n\n'))
  print(Style.RESET_ALL)
  if atkcheck == 'y':
    txt('You attack the guard')
  else:
    txt('You decide to not attack the guard')
    txt('[!]The guard wakes up! You could not sneak past...')
    txt('[!]The guard sees you!')
  txt('You hit the guard')
  txt('The guard slumps over')
def askdirection(direction):
  qna = str(input(Fore.BLUE+'y/n\n'))
  print(Style.RESET_ALL)
  if qna == 'y':
    txt('You decide to go north')
    direction = 'north'
  elif qna == 'n':
    direction = 'south'
allrooms = ['cell','hallway','armory1','armory2','secret']
knownrooms = ['cell' 
              ,'hallway'
              ,'armory1']
currentroom = 'cell'
currentitems = ['hairpen',]
inventory = []
hp = 100
txt('You wake up in a damp room...')
txt('You look around...')
txt1('...')
txt('Suddenly you remember, you were taken here when you were framed for a murder')
txt('You have to escape!')
txt(f'You are in the {currentroom}')
txt(f'You look around and see a {currentitems[0]}')
askgrab(currentitems[0])
txt('You go over to the door.')
txt('To unlock the door you will have to pick it with the hairpin')
locknum = random.randint(1,100)
unlock(locknum)
txt('You walk out and peek around the new room')
txt('[NEW ROOM UNLOCKED: Hallway]')
currentroom = allrooms[1]
time.sleep(6/5)
txt('You hear snoring and look to the sound')
txt('[!]There is a sleeping guard to your left')
attack()
txt('Do you want to go north?')
txt('Yes for north, no for south.')
time.sleep(2)
replit.clear()
direction = ''
askdirection(direction)
if direction == 'north':
  txt('You end up in a barracks')
  txt("[!]There's a guard watching the room")
  txt('Do you wish to attack?')
  qna = str(input(Fore.BLUE+'y/n\n'))
  while True:
    if qna == 'y':
      if inventory[1] == 'sword' and inventory[2] == 'shield':
        txt(Style.RESET_ALL+'You run at the guard')
        txt('The guard swings, but you block with your shield')
        txt('You swing at the guard')
        txt('The guard blocks you and pulls out a dagger.')
        txt('You slice the guard just as he stabs you in the hip')
        hp -= 40
        txt('[!]You are bleeding, you have lost 40 hp...')
        txt('The guard passed out')
        txt1('...')
        break
      else:
        txt(Style.RESET_ALL+"You don't have a weapon...")
        txt('The guard slits your throat...')
        txt('The End...')
        txt(Fore.RED+'[Ending 2 of 3: The Unprepared]')
        exit()
    elif qna == 'n':
      txt(Style.RESET_ALL+'You sneak around avoiding the guard')
      txt('You grab a sword and shield.')
      inventory.append('sword')
      inventory.append('shield')
  txt('You survived and leave the armory')
  txt('You are walking out when something catches your eye...')
  txt('A map!')
  txt('Do you want to grab it?')
  qna = str(input(Fore.BLUE+'y/n\n'))
  if qna == 'y':
    txt(Style.RESET_ALL+'You grab the map...')
    txt('The map is titled "Vents"')
    inventory.append('map')
    txt('You look over the map and see this:')
    print(Back.LIGHTYELLOW_EX)
    # unicode: ═ β•‘ β•” ╦ β•— β•  ╬ β•£ β•š β•© ╝ ⚐
    txt('⚐A B C D E F')
    txt('1╔════╦═╦══╗')
    txt('2║╔╦~~╠═╣? β•‘')
    txt('3β• β•β•šβ•#╬═╬╝ β•‘')
    txt('4β•‘~~~~~~~~~β•‘')
    txt('5β•šβ•β•β•β•©β•β•β•β•β•β•')
    txt(Style.RESET_ALL+'Some of the map is blurred out (~)')
    txt('The vent areas are marked by the paths')
    txt('The # is the cell, and the ? looks something like a throne...')
  else:
    txt('You decide against taking the map...')
elif direction == 'south':
  # Not done yet
  txt('[!]Uncompleted')
  txt1('...')
  exit()
1 Like

Are there any errors in the console when the program ends? Can you be more specific where the program stops?

Also, use sys.exit() instead of exit().

There are no errors but it shows that it is finished when it isn’t done yet.

It stops around line 60 in the askdirection(direction) function

Sorry, to make it easier I should say the fifth line of the function.

The reason is that your askdirection function does not work, or at least it does not work in the way you think.
When you set direction to a value inside your askdirection function, it does not change the value of the direction variable in the module. The only thing it does is set the direction local variable that is inside askdirection to a different value. (There are really two different direction variables, one of them is in the global scope (module level) and the other is in local scope (function level).)

Two options:

Return a value from askdirection

def askdirection():
  inp = input('...')
  if inp == 'y':
    txt('...')
    return 'north'
  if inp == 'n':
    return 'south'
  print('invalid input')
  return askdirection()  # <- using a loop is better than recursive call to function
direction = askdirection()
if direction == 'north':
  ...

Or, directly set the global direction variable using the global keyword (not recommended)

def askdirection():
  global direction
  inp = input('...')
  if inp == 'y':
    txt('...')
    direction = 'north'
  elif inp == 'n':
    direction = 'south'
  else:
    print('invalid input')
    askdirection()  # <- using a loop is better than recursive call to function
direction = ''
askdirection()
if direction == 'north':
  ...
4 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.