dawsondiaz
dawsondiaz

Reputation: 684

Python Text Adventure Loop

I am making a very simple text-adventure game in python 3.3.4, and would like to know if there would be any possible way to make it when input does not match a/b it returns to the print command where the question is asked.

Here is what I have currently:

import time
import sys
from random import randrange

text = "** Execute Long Intro **"

for c in text:
    sys.stdout.write(c)
    sys.stdout.flush()
    seconds = "0." + str(randrange(1, 2, 1))
    seconds = float(seconds)
    time.sleep(seconds)


time.sleep(1)


obj1 =input('\nDo you \na.) Rest on the ground \nb.) Find a way out of the jungle\n')
if obj1 in ('a'):
    print('You find a comfortable spot on the ground and drift into sleep...')
    time.sleep(.6)
    print('Zzz.\nZzz..\nZzz...')
    time.sleep(3)
    print('You wake to a strange noise, and work your way out of the jungle.') 
    time.sleep(1)

    print('You emerge out of the jungle and walk along the shoreline of a sunny beach')
    time.sleep(1)
    print('** Objective One Completed **')
    time.sleep(2)



elif obj1 == 'b':
    print('You manage to find a path out of the jungle and discover a beach,')
    time.sleep(1)
    print('** Objective One Completed **')
    time.sleep(2)

elif obj1 != ('a','b'):
    print('Uh.')

Help would be greatly appreciated.

Upvotes: 1

Views: 1001

Answers (2)

Farshid Ashouri
Farshid Ashouri

Reputation: 17719

You can try using blessings library. install it with:

pip install blessings

and the usage is simple:

from blessings import Terminal
term = Terminal()
print term.clear_bol
print '\t{t.yellow}action{t.normal}|{t.green}Done!{t.normal}'.format(t=term)

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34176

Use a while loop:

obj1 = input(...)

while obj1 not in ('a', 'b'):
    obj1 = input("Invalid. Enter again: ")

if obj1 == 'a':
    ...
elif obj1 == 'b':
    ...

Upvotes: 2

Related Questions