9
\$\begingroup\$

I'm new to programming, and I would like to know if this is the right way to layout code. If anyone has any tips, please share them.

import random
import time

def displayIntro():
    print("You are in a land full of dragons. In front of")
    print("you are two caves. In one cave the dragon is")
    print("friendly and will give you his treasure. In the")
    print("other, the dragon is greedy and hungry and will")
    print("eat you on sight")
    print()

def chooseCave():
    cave = ""
    while cave != "1" and cave != "2":
        print("Which cave will you choose? (1 or 2)")
        cave = input()
   return cave

def checkCave(chosenCave):
    print("You approach the cave...")
    time.sleep(2)
    print("It is dark and spooky...")
    time.sleep(2)
    print("A large dragon jumps out in front of you. He opens his jaws and...")
    print()
    time.sleep(2)

    friendlyCave = random.randint(1,2)

    if chosenCave == str(friendlyCave):
        print("gives you his treasure")
    else:
        print("gobbles you down in one bite")


playAgain = "yes"
while playAgain =="yes" or playAgain == "y":

    displayIntro()

    caveNumber = chooseCave()

    checkCave(caveNumber)

    print("Would you like to play again? (yes or no)")
    playAgain = input()
\$\endgroup\$
  • \$\begingroup\$ General rules for code layout in Python are stated in PEP 8 -- Style Guide for Python Code. \$\endgroup\$ – Changaco Aug 28 '13 at 21:47
  • 3
    \$\begingroup\$ This is one of the most suspenseful code examples I've come across. My main bits of advice would be to use a main function instead of the raw script style execution and to store the chosen cave as an int instead of as a string, and then do int comparison instead of string comparison though. \$\endgroup\$ – scott_fakename Aug 29 '13 at 4:20
1
\$\begingroup\$

In addition to other answers you can use 'in' to compare for more than one value.

So

while cave != "1" and cave != "2":
    ...

becomes:

while cave not in ["1", "2"]:
    ...

or even:

caves = set("12")
while cave not in caves:
    ...
\$\endgroup\$
  • \$\begingroup\$ a set is a more natural container to check presence \$\endgroup\$ – Maarten Fabré Apr 12 '18 at 7:42
  • \$\begingroup\$ That is true. while cave not in Set(["1", "2"]): \$\endgroup\$ – Graeme Stuart Apr 13 '18 at 9:19
  • 1
    \$\begingroup\$ since python 2.4 there is a set builtin, so {'1', '2'} or set('12') works too \$\endgroup\$ – Maarten Fabré Apr 13 '18 at 9:38
13
\$\begingroup\$

(1) For variable and function names, check_cave (underscore) rather than checkCave (camelcase) are standard in python.

(2) The line def chooseCave needs to be unindented

(3) Your main game loop could probably be neatened up:

while True:
    display_intro()
    check_cave(choose_cave())
    print("Would you like to play again? (yes or no)")
    if input() not in ('yes', 'y'):
        break

(4) conventionally you should put the main game loop in a function called main() and add

if __name__ == "__main__":
    main()

at the end.

(5) More a matter of taste, but I'd find it more elegant to write choose_cave like this:

def choose_cave():
    while True:
        print("Which cave will you choose? (1 or 2)")
        cave = input()
        if cave in ('1', '2'):
            return cave
\$\endgroup\$
6
\$\begingroup\$

print and multiline text

Your displayIntro uses several print calls to output multiline text. You may consider rewriting them like this:

def displayIntro():
    print("""
You are in a land full of dragons. In front of
you are two caves. In one cave the dragon is
friendly and will give you his treasure. In the
other, the dragon is greedy and hungry and will
eat you on sight"""[1:])

This way you can easily apply 80 columns rule for your program output.

""" operator starts (and ends) multiline string. [1:] trims leading newline.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.