HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Tiny text adventure

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textadventuretiny

Problem

I made a small text adventure. I'm not trying to make it appealing to anyone, but rather just to practice my Python skills. I am fairly new to Python and this was my next step after making a console calculator and BMI calculator. I would like someone to tell me how this code could be better optimised, less work, etc.

```
import time
import random

# game function
def game():

print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print ("Welcome to the cavern of secrets!")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")

time.sleep(3)

print ("You enter a dark cavern out of curiosity. It is dark and you can only make out a small stick on the floor.")
ch1 = str(input("Do you take it? [y/n]: "))

# STICK TAKEN
if ch1 in ['y', 'Y', 'Yes', 'YES', 'yes']:
print("You have taken the stick!")
time.sleep(2)
stick = 1

# STICK NOT TAKEN
else:
print("You did not take the stick")
stick = 0

print ("As you proceed further into the cave, you see a small glowing object")
ch2 = str(input("Do you approach the object? [y/n]"))

# APPROACH SPIDER
if ch2 in ['y', 'Y', 'Yes', 'YES', 'yes']:
print ("You approach the object...")
time.sleep(2)
print ("As you draw closer, you begin to make out the object as an eye!")
time.sleep(1)
print ("The eye belongs to a giant spider!")
ch3 = str(input("Do you try to fight it? [Y/N]"))

# FIGHT SPIDER
if ch3 in ['y', 'Y', 'Yes', 'YES', 'yes']:

# WITH STICK
if stick == 1:
print ("You only have a stick to fight with!")
print ("You quickly jab the spider in it's eye and gain an advantage")
time.sleep(2)
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print (" Fighting... ")
print (" YOU MUST HIT ABOVE A 5 TO KILL THE SPIDER ")
print ("IF THE SPIDER HITS HIGHER THAN YOU, YOU WILL DIE")
print ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
t

Solution

You have a lot of repetition in your code, which is something you should focus on reducing.
For example, this recurring pattern:

ch2 = str(input("Do you approach the object? [y/n]"))

# APPROACH SPIDER
if ch2 in ['y', 'Y', 'Yes', 'YES', 'yes']:


... can be encapsulated into a function and reused:

def ask(question):
    answer = input(question + " [y/n]")
    return answer in ['y', 'Y', 'Yes', 'YES', 'yes']

if ask("Do you approach the object?"):
    # ...


Another pattern is printing followed by time.sleep:

print ("You approach the object...")
    time.sleep(2)
    print ("As you draw closer, you begin to make out the object as an eye!")
    time.sleep(1)
    print ("The eye belongs to a giant spider!")


which could be reduced to this:

def print_pause(lines):
    for line, pause in lines:
        print line
        time.sleep(pause)

print_pause([
    ("You approach the object...", 2),
    ("As you draw closer, you begin to make out the object as an eye!", 1),
    ("The eye belongs to a giant spider!", 0)
    ])


The spider fight part also contains a lot of repetition, but I will leave it as an exercise for you to turn it into a function and reuse. :)

Code Snippets

ch2 = str(input("Do you approach the object? [y/n]"))

# APPROACH SPIDER
if ch2 in ['y', 'Y', 'Yes', 'YES', 'yes']:
def ask(question):
    answer = input(question + " [y/n]")
    return answer in ['y', 'Y', 'Yes', 'YES', 'yes']

if ask("Do you approach the object?"):
    # ...
print ("You approach the object...")
    time.sleep(2)
    print ("As you draw closer, you begin to make out the object as an eye!")
    time.sleep(1)
    print ("The eye belongs to a giant spider!")
def print_pause(lines):
    for line, pause in lines:
        print line
        time.sleep(pause)

print_pause([
    ("You approach the object...", 2),
    ("As you draw closer, you begin to make out the object as an eye!", 1),
    ("The eye belongs to a giant spider!", 0)
    ])

Context

StackExchange Code Review Q#36768, answer score: 16

Revisions (0)

No revisions yet.