I have just recently picked up coding and am trying to improve my coding. I've written this bit of code to create a little adventure world. I haven't completed the code, everything runs how I want it too but I'm aware this code isn't efficient. I would like some advice and some help on how I would make my code better, particularly more efficient. Also, I have not coded further from guessing the number from the box, so it just exits the code after.
Anything would be appreciated, as I know I have a lot to learn!
import random
attack = 0
health = 0
element = 0
def character():
number = int(input("""Please choose your character.
\n1. You control the element of fire, and bend it to your will freely.
\n2. You control the element of water, you feel the flow and control it elegantly.
\n3. You control the element of ice, you're power is the to call on ice and use it lethally.
> """))
if number == 1:
choice = 1
fire_character()
elif number == 2:
choice = 2
water_character()
elif number == 3:
choice = 3
ice_character()
else:
print("Sorry you can only choose a number between 1 and 3.")
def fire_character():
global attack
global health
global element
attack += 100
health += 50
element = 'fire'
print(f"\nSince you have chosen fire: Attack = {attack}, Health = {health}")
def water_character():
global attack
global health
global element
attack = 75
health = 75
element = 'water'
print(f"\nSince you have chosen water: Attack = {attack}, Health = {health}")
def ice_character():
global attack
global health
global element
attack = 50
health = 100
element = 'ice'
print(f"\nSince you have chosen ice: Attack = {attack}, Health = {health}")
def welcome(attack, health, element):
print(f"""
Welcome to the game! You have chosen the {element} element! Lets see how far you can get!
Best of luck, I hope the game ends up well!
""")
def which_way():
print(f"With your newly discovered {element} powers, you mysteriously wake up in a whole new world.")
print("You're in a crowded forest and there's noises coming from every direction.")
print("A small furry animal approaches you and startles you!")
print(""" 'DO NOT WORRY SIR, I NO HARM YOU! I HEAR TO GUIDE YOU THROUGH NEW PLACE', the little creature says. """)
print("The creature then tells you he seems to have forgotten the safe way through... He says he has a foggy idea.")
print("He will reccomend which way to go he says.. but he says not to trust his memory...")
print("\n The furry guy reccomends going left.")
loop = False
while loop == False:
choice = input("Are you going to go left or right? > ").lower()
if choice == 'right':
enemy1()
loop = True
elif choice == 'left':
print("\nYou decide to listen to the lil fella and go left. You walk through a relaxing walkway in the forest.")
print("You look around and see a gorgeous lake and beautiful animals sitting around it.")
print("You peacfully walk around for a while, admiring all of the natures beauty and then eventually carry on.")
choice_two()
loop = True
else:
print("Thats not an option sorry ")
def enemy1():
global attack
global health
global element
en_attack = 20
en_health = 100
en_element = 'ice'
print("\nYou go against your little companions advice and turn right.")
print("As you walk through the forest you hear sounds coming out of the bushes.")
print("An enemy with frost emitting from around his body slowly walks out...")
print("You're little body comes upto you and tells you about the character.")
print("THIS ICE GUY WEAK HAHA, HE STATS ARE:")
print(f"Attack: {en_attack}, Health: {en_health}, Element: {en_element}")
loop = False
while loop == False:
choice = input("\nWhat do you want to do? Attack, run or try talk it out? (attack, run or talk): ").lower()
if choice == 'attack':
print(f"You throw a {element} ball at him!")
if element == 'fire':
print("Your fire ball does extra damage!")
print(f"\nEnemy Health: {en_health} - Your attack: {attack}")
en_health = en_health - attack
print(f"The enemy took an extra 10 damage beacuse of the element difference!")
en_health = en_health - 10
print(f"The enemys health is at {en_health}! He melts into a puddle as you carry on.")
loop = True
if en_health <= 0:
print("Enemy falls on the ground and dies.")
choice_two()
if element == 'water':
print("Your water ball does terribe damage!")
print(f"\nEnemy Health: {en_health} - Your attack: {attack}")
en_health = en_health - attack
print(f"The enemy uses your water and hardens his armor back up a bit... Enemy adds 10 health back")
en_health = en_health + 10
print(f"The enemys health is at {en_health}!")
if en_health <= 0:
print("Enemy falls on the ground and dies.")
choice_two()
print(f"\nThe enemy wasn't happy that you just attacked him out of nowhere and attacks you!")
health = health - en_attack - 10
print(f"The enemy hardens the water around it and turns them into icyicles launching them at you doing {en_attack} damage!")
print(f"The ice enemies attack does an extra 10 damage..")
print(f"Your health is {health}")
if element == 'ice':
print("Your ice ball does neutral damage!")
print(f"\nEnemy Health: {en_health} - Your attack: {attack}")
en_health = en_health - attack
print(f"The enemys health is at {en_health}!")
if en_health <= 0:
print("Enemy falls on the ground and dies.")
choice_two()
print(f"\nThe enemy wasn't happy that you just attacked him out of nowhere and attacks you!")
health = health - en_attack
print(f"The enemy hardens the water around it and turns them into icyicles launching them at you doing {en_attack} damage!")
print(f"Your health is {health}")
loop = False
elif choice == 'run':
print("\nYou runaway with your tail between your legs!")
die("While running away you trip into a hole of acid. Shame.")
loop = True
elif choice == 'talk':
print("\nWhen the beast gets close to you, you calmly ask for his name.")
print("The beast tells you his name and then you guys chat for a while.")
print("When you guys finishing talking about past lives he kindly lets you stroll on past.")
choice_two()
loop = True
else:
print("Sorry that's not an option.")
loop = False
def choice_two():
global health
print(f"\n You're health is {health}")
print("\n While you are strolling a mysterious blue box appears. You approach the box and start inspecting it.")
print("Your little guide starts going on a bit how you have to guess a number to open it. But there's a catch... ")
print("If you don't guess the number right you get inflicted with an uncurable poison. You get 3 guesses.")
print("The little guy says guess between 1 and 8...")
if element == 'water':
print(f"\nAs you aproach the box it starts lighting up a bit.. It seems to connect with your element {element}.")
print("Guess between 1 and 5.")
box_number_notwater = random.randint(1, 8)
box_number_water = random.randint(1, 5)
guess_count = 0
while guess_count <= 2:
if element == 'fire' or 'ice':
guess = int(input("Guess a number: "))
if guess == box_number_notwater:
print("You got it right!")
exit()
elif guess < box_number_notwater:
print("Sorry! Try guessing higher!")
guess_count += 1
elif guess > box_number_notwater:
print("Sorry! Try guessing lower!")
guess_count += 1
elif element == 'water':
guess = int(input("Guess a number: "))
if guess == box_number_water:
print("You got it right!")
exit()
elif guess < box_number_water:
print("Sorry! Try guessing higher!")
guess_count += 1
elif guess > box_number_water:
print("Sorry! Try guessing lower!")
guess_count += 1
if guess_count == 3:
die('A squirrel comes from the box and bites your finger. You start slowly dosing off and die.')
def die(reason):
print("")
print(f"{reason} Good Job!!")
exit()
player = input("Would you like to play the game? ").lower()
if player == 'yes':
character()
else:
print("Goodbye!")
exit()
welcome(attack, health, element)
which_way()