I'm a beginner in python and my code looks a little cluttered and a lot of lines are being duplicated, so I was wondering if I could get some suggestions on how to declutter and improve?
import time, random
def start():
global user
global character
global shadow
user = {'gold': 0, 'HPP':1, 'EPP': 1}
character = {'HP': 100, 'EP': 100, 'MP': 0, 'XP': 0}
shadow = {'HP': 50}
print("Type H at any time for help.")
locations()
def locations():
Locationsearch = input("Where would you like to go now? ")
if Locationsearch.lower() == "shop":
shop()
elif Locationsearch.lower() == "dungeon":
dungeon()
elif Locationsearch.lower() == "h":
print("Locations:")
print("Shop")
print("Dungeon")
locations()
def shop():
shopping = input("What would you like to purchase? ")
if shopping.lower() == "hpp":
if user['gold'] >= 7:
user['gold'] -= 7
user['HPP'] += 1
print("You now have " + str(user['HPP']) + " HPPs and " + str(user['gold']) + " gold.")
shop()
elif shopping.lower() == "epp":
if user['gold'] >= 10:
user['gold'] -= 10
user['EPP'] += 1
print("You now have " + str(user['EPP']) + " EPPs and " + str(user['gold']) + " gold.")
shop()
elif shopping.lower() == "h":
print("Type C for a catalogue and B to go back.")
shop()
elif shopping.lower() == "c":
print("HPP (7g)")
print("EPP (10g)")
shop()
elif shopping.lower() == "b":
locations()
else:
print("Sorry, I do not understand. Type H for help.")
shop()
def dungeon():
monsterspin = 1
if monsterspin == 1:
print("You encounter a shadow!")
shadowfight()
def shadowfight():
if shadow['HP'] <= 0:
print("Congrats! You got 10 gold!")
user['gold'] += 10
dungeonq = input("Would you like to continue or go back? ")
shadow['HP'] = 50
if dungeonq.lower() == "continue":
dungeon()
elif dungeonq.lower() == "back":
locations()
elif character['HP'] <= 0:
death()
fighting = input("What do you do? ")
if fighting.lower() == "fight":
shadow['HP'] -= random.randint(7, 10)
print("Shadow has " + str(shadow['HP']) + " health.")
character['HP'] -= random.randint(6, 9)
print("The shadow strikes back! You have " + str(character['HP']) + " HP!")
shadowfight()
elif fighting.lower() == "items":
print("You have " + str(user['HPP']) + " HPPs and " + str(user['EPP']) + " EPPs. ")
itemq = input("Type what you would like to use or type B to go back. ")
if itemq.lower() == "hpp":
if user['HPP'] >= 1:
user['HPP'] -= 1
character['HP'] += 30
if character['HP'] > 100:
character['HP'] = 100
print("You used a HPP and now have " + str(character['HP']) + " HP.")
character['HP'] -= random.randint(6, 9)
print("The shadow strikes! You have " + str(character['HP']) + " HP!")
shadowfight()
else:
print("You cannot preform this action!")
shadowfight()
if itemq.lower() == "epp":
if user['EPP'] >= 1:
user['EPP'] -= 1
character['EP'] += 30
if character['EP'] > 100:
character['EP'] = 100
print("You used a EPP and now have " + str(character['EP']) + " EP.")
character['HP'] -= random.randint(6, 9)
print("The shadow strikes! You have " + str(character['HP']) + " HP!")
shadowfight()
else:
print("You cannot preform this action.")
shadowfight()
elif fighting.lower() == "defend":
character['HP'] -= random.randint(3, 4)
print("The shadow strikes but you block! You have " + str(character['HP']) + " HP!")
shadowfight()
elif fighting.lower() == "moves":
def moves():
print("Here are your unlocked moves:")
print("Void Slash (40EP)- Slash at 1 enemy doing a moderate amount of damage.")
movechoice = input("Choose a move or type B to return to the fight. ")
if movechoice.lower() == "void slash":
if character['EP'] >= 110:
character['EP'] -= 110
shadow['HP'] -= random.randint(300, 400)
print("Shadow has " + str(shadow['HP']) + " health.")
shadowfight()
else:
print("You don't have enough EP for this action!")
moves()
elif movechoice.lower() == "b":
print("You return to the fight.")
shadowfight()
else:
print("I don't understand. Try again.")
moves()
moves()
elif fighting.lower() == "run":
runchance = random.randint(1,3)
if runchance == 1:
character['HP'] -= random.randint(6, 9)
print("You tried to run but failed! You have " + str(character['HP']) + " HP!")
shadowfight()
else:
print("You ran successfully.")
locations()
elif fighting.lower() == "h":
print("Available commands are: Attack, Defend, Moves, Items, and Run")
shadowfight()
def death():
deathq = input("You died. Try Again? y/n")
if deathq.lower() == "y":
start()
elif deathq.lower() == "n":
print()
start()