5
\$\begingroup\$

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()
\$\endgroup\$
4
  • \$\begingroup\$ What happens when you say "n" when you die? \$\endgroup\$ Commented Apr 14, 2022 at 0:01
  • \$\begingroup\$ that's my bad, forgot to add it, but it should just close the program \$\endgroup\$
    – vCian
    Commented Apr 14, 2022 at 0:05
  • \$\begingroup\$ But it doesn't end the program, though? \$\endgroup\$ Commented Apr 14, 2022 at 0:07
  • \$\begingroup\$ right, i put print as a placeholder until i added an end sequence so it didnt show up as an error while running \$\endgroup\$
    – vCian
    Commented Apr 14, 2022 at 0:10

1 Answer 1

1
\$\begingroup\$

This is my opinion, just to improve the readability of the code.

You can cut several consecutive prints down to one without losing the beauty.

print("Locations:")
print("Shop")
print("Dungeon")

Try:

print("Locations:", "Shop", "Dungeon", sep='\n')

Using plus is not a mistake, but in my opinion it clutters the code. It might be useful to use f' '.

print("Shadow has " + str(shadow['HP']) + " health.")

Try:

print(f"Shadow has {str(shadow['HP'])} health.")

See, please.

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

In the code, you are implying that the player entered some value, but that may not be the case. Also, you are not sure if the player entered correctly or not. A good rule of thumb to remember is that if there is an opportunity to do it wrong, the user will do just that.

See, please.

print("Type H at any time for help.")

Below:

elif Locationsearch.lower() == "h":

You are prompting the user to press "H", but expect them to press "h".

If you're prompting the user, it's better to be correct. But that's also a signal to the question above, you don't check what value is entered. This is important, because in more serious programs you can accidentally delete user data or do something else, something more serious.

I think, it's good practice to get familiar with the PEP8 standard.

Make empty lines in your code, try to write in a line that is 78 characters long. At first, it is not clear why, but I think, with the habit you understand that it is easier to read.

\$\endgroup\$
1
  • 4
    \$\begingroup\$ {str(shadow['HP'])} is not necessary. All format fields imply a str cast. \$\endgroup\$
    – Reinderien
    Commented Sep 14, 2022 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.