Извините за длинный код, но я чувствовал, что важно включить в себя то, что я пытаюсь выполнить. Я новичок в Python и программировании в целом, и я пытался создать простую текстовую приключенческую игру. Сначала игра работала хорошо, пока я не добавил встречу с пчелами. Я запустил программу, и я решил бежать от медведя, поэтому мой hp должен быть в 40, который был отображен. Однако, когда я выбрал плетение пчел, мой hp должен быть равен 0, потому что 40 (мой текущий hp) -40 = 0. Однако мой hp отображается в 60, как будто встреча медведей никогда не происходила. Есть ли способ исправить это или это ограничение в Python?Как передать переменную между функциями в Python
from sys import exit
from time import sleep
import time
#Hp at start of game:
hp = 100
#The prompt for inputs
prompt = "> "
#Bear encounter
def bear(hp):
choice = raw_input("> ")
if "stand" in choice:
print "The bear walks off, and you continue on your way"
elif "run" in choice:
print "..."
time.sleep(2)
print "The bear chases you and your face gets mauled."
print "You barely make it out alive, however you have sustained serious damage"
hp = hp-60
currenthp(hp)
elif "agressive" in choice:
print "..."
time.sleep(2)
print "The bear sees you as a threat and attacks you."
print "The bear nearly kills you and you are almost dead"
hp = hp-90
currenthp(hp)
else:
print "Well do something!"
bear(hp)
#Bee encounter
def bee(hp):
choice = raw_input(prompt)
if "run" in choice:
print "..."
sleep(2)
print "The bee flies away and you continue on your way."
currenthp(hp)
elif "swat" in choice:
print "..."
sleep(1)
print "You succesfully kill the bee. Good Job!"
sleep(1)
print "Wait a minute"
sleep(2)
print "The bee you killed gives off pheremones, now there are hundreds of bees chasing you."
print "The bees do some serious damage."
hp = hp-40
sleep(1)
currenthp(hp)
else:
print "Well, do something."
bee(hp)
#Function to display the current hp of the current player
def currenthp(hp):
if hp < 100:
print "Your hp is now at %d" % hp
elif hp <= 0:
dead()
else:
print "You are still healthy, good job!"
#Called when player dies
def dead():
print "You sustained too much damage, and as a result have died."
time.sleep(3)
print "GAME OVER!"
print "Would you like to play again?"
choice = raw_input("> ")
if "y" in choice:
start_game()
else:
exit(0)
#Called to Start the Game, useful for restarting the program
def start_game():
print "Welcome to Survival 101"
#START OF GAME
start_game()
print "You start your regular trail."
print "It will be just a little different this time though ;)"
time.sleep(3)
print "You are walking along when suddenly."
time.sleep(1)
print "..."
time.sleep(2)
#Start of first encounter
print "Wild bear appears!."
print "What do you do?"
print "Stand your ground, Run away, be agressive in an attempt to scare the bear"
#first encounter
bear(hp)
#Start of second encounter
print "You continue walking and see a killer bee approaching you"
print "What do you do"
print "run away, swat the bee away"
bee(hp)
use return to pass variable to caller – user3570335
Как в стороне ... это выглядит как действительно классный проект для ребенка. Вы заинтересованы в обучении моего 12-летнего ребенка (за плату), помогая ему написать такую игру? (Я знаю тему, но у Бернарда нет контактной информации на его странице). –
Я на самом деле только восьмой класс, но пытаюсь научиться кодировать. Я хочу быть программистом в будущем. –