简体   繁体   中英

Python Code not executing pieces of code

I am using a program from Invent with Python, the dragon realm one, to make my own story. For some reason, the code starting from chooseVillage to the play again part is not working. It just doesn't show. Can you please help me? Thanks in advance.

The code is as shown below

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 chooseCave():
    cave = ''
    while cave != '1' and cave != '2':
        print('Which cave will you go into? (1 or 2)')
        cave = input()

        return cave

def checkCave(chosenCave):
    print('You approach the cave...')
    time.sleep(2)
    print('It is dark and spoopy...')
    time.sleep(2)
    print('A large dragon jumps out in front of you! He looks at you and...')
    print()
    time.sleep(2)

    friendlyCave = random.randint(1,2)

    if chosenCave == str(friendlyCave):
        print('Gives you his treasure')
        print('You decide to spend it at a village')
    else:
        print('Gobbles you down in one bite.')
        quit()

def chooseVillage():
  village = '' 
  while village != '1' and village != '2' and village != '3':
    print('Which village will you go to? (1, 2, or 3)')
    village = input()

def checkVillage(chosenVillage):
  print('You approch the city')
  time.sleep(2)
  print('It is filled with many stores,')
  time.sleep(2)
  print('In one store, a man is standing at it...')
  time.sleep(2)
  print('He has a gun, and has his hand by it')
  time.sleep(2)
  print('He says Welcome to my store,')
  print()
  time.sleep(2)

  friendlyVillage = random.randint(1, 2, 3)

  if chosenVillage == str(friendlyVillage):
    print('And he sells you some weapons.')
  else:
    print('He looks at you and says:')
    time.sleep(2)
    print('You are not from around here,')
    time.sleep(4)
    print('And he shoots you! Better luck next time')
    quit()


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()

Link to this program in repl.it here .

The function chooseVillage is not being executed at any point. If you play it through, then say no for play again, you can execute directly chooseVillage() from the command line to run the code which does that. I guess what you want to do to modify it is, before the caveNumber = chooseCave() , add in print('Do you want to play cave or village?') and then use the input there to either run chooseCave() or chooseVillage()

If you search through the code for chooseVillage() you will see it is only instantiated once, which is when the function is def chooseVillage(): and not executed subsequently.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM