r/pythonhelp • • Sep 18 '21

Help needed with Cave Game

Hey y'all, so I'm trying to create a cave game where it prompts the user for their character name, and gives them a choice of going through three caves; and based on what they pick, they get a random outcome. I keep getting an error when I'm trying to insert a variable to print out a name in a statement, and am just curious if my code even makes sense, would any of you be able to help out?

import random

import time

def displayIntro():

print('You are in a land full of dragons. In front of you,')

print('you see two caves. In one cave, the dragon is friendly')

print('and will share his treasure with you. The other dragon')

print('is greedy and hungry, and will eat you on sight.')

print()

def askname():

print("What would you like to call your character?")

n=input()

def chooseCave():

cave = ''

while cave!= '1' and cave!= '2' and cave!= "3":

print("Which cave should",n,"go into? (1, 2, or 3)")

cave = input()

return cave

def checkCave(chosenCave):

print(n,"approaches the cave...")

time.sleep(2)

print('It is dark and spooky...')

time.sleep(2)

print("A large dragon jumps out in front of",n,"!He opens his jaws and...")

print()

time.sleep(2)

friendlyCave = random.randint(1, 3)

convoCave = random.randint(1,3)

if chosenCave == str(friendlyCave):

print('Gives you his treasure!')

if chosenCave == str(convoCave):

print('Starts talking to you!')

else:

print('Gobbles you down in one bite!')

playAgain = 'yes'

while playAgain == 'yes' or playAgain == 'y':

displayIntro()

caveNumber = chooseCave()

checkCave(caveNumber)

print('Do you want to play again? (yes or no)')

playAgain = input()

Thanks for any help, really appreciate it!

2 Upvotes

3 comments sorted by

3

u/skellious Sep 18 '21

You need to use + instead of comma to join the text in your print statement. Python print takes one argument for content, things after the comma are taken as other variables.

Better still, read up on f strings.

Example:

print(f"Hello, my name is {name} and I like to read {genre} books the most!")

2

u/skellious Sep 18 '21 edited Sep 19 '21

By the way, if you cast your input put to an int you won't have to use str(friendlyCave)

cave = int(input())

Also you can put your prompt in the same line as your input, such as:

cave = int(input(f"Which cave should {n} go into? (1, 2, or 3)"))