How do I improve my code?

Question:
I wish that when a monster spawns, I have to fight it, but instead it just deals damage and then disappears. If it’s easy to say - I want to be able to fight, but I don’t know how to do it.

Repl link:(https://replit.com/@kk284/Miner-Upgrade-Clicker)

import os as s
import random as r
import time as t

print("Miner Resource Upgrade")
print("----------")
print("s-shop l-lottery h-healshop hack-try to get password legitely:)")
power = 0
balance = 0
cost = 5
pickaxePower = 0
health = 100
input()
s.system("clear")


def resources2():
  global balance
  if power == 2:
    resourceType = ["Coal", "Copper"]
    resourceTypeChoice = r.choice(resourceType)
    if resourceTypeChoice == "Copper":
      balance += 1
    else:
      balance += 0.5
  elif power == 5:
    resourceType = ["Coal", "Copper", "Iron"]
    resourceTypeChoice = r.choice(resourceType)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    else:
      balance += 0.5
  elif power == 8:
    resourceType = ["Coal", "Copper", "Iron", "Gold"]
    resourceTypeChoice = r.choice(resourceType)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    else:
      balance += 0.5
  elif power == 12:
    resourceType = ["Coal", "Copper", "Iron", "Gold", "Titanum"]
    resourceTypeChoice = r.choice(resourceType)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    elif resourceTypeChoice == "Titanium":
      balance += 7
    else:
      balance += 0.5
  elif power == 15:
    resourceType = ["Coal", "Copper", "Iron", "Gold", "Titanium", "Diamond"]
    resourceTypeChoice = r.choice(resourceType)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    elif resourceTypeChoice == "Titanium":
      balance += 7
    elif resourceTypeChoice == "Diamond":
      balance += 10
    else:
      balance += 0.5
  elif power == 20:
    resourceType = [
      "Coal",
      "Copper",
      "Iron",
      "Gold",
      "Titanium",
      "Diamond",
      "Uranium",
    ]
    resourceTypeChoice = r.choice(resourceType)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    elif resourceTypeChoice == "Titanium":
      balance += 7
    elif resourceTypeChoice == "Diamond":
      balance += 10
    elif resourceTypeChoice == "Uranium":
      balance += 50
    else:
      balance += 0.5
  else:
    resourceType = ["Coal"]
    resourceTypeChoice = r.choice(resourceType)
    balance += 0.5


def resources():
  global balance
  if power == 2:
    resourceType = ["Coal", "Copper"]
    resourceTypeChoice = r.choice(resourceType)
    print(resourceTypeChoice)
    if resourceTypeChoice == "Copper":
      balance += 1
    else:
      balance += 0.5
  elif power == 5:
    resourceType = ["Coal", "Copper", "Iron"]
    resourceTypeChoice = r.choice(resourceType)
    print(resourceTypeChoice)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    else:
      balance += 0.5
  elif power == 8:
    resourceType = ["Coal", "Copper", "Iron", "Gold"]
    resourceTypeChoice = r.choice(resourceType)
    print(resourceTypeChoice)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    else:
      balance += 0.5
  elif power == 12:
    resourceType = ["Coal", "Copper", "Iron", "Gold", "Titanum"]
    resourceTypeChoice = r.choice(resourceType)
    print(resourceTypeChoice)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    elif resourceTypeChoice == "Titanium":
      balance += 7
    else:
      balance += 0.5
  elif power == 15:
    resourceType = ["Coal", "Copper", "Iron", "Gold", "Titanium", "Diamond"]
    resourceTypeChoice = r.choice(resourceType)
    print(resourceTypeChoice)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    elif resourceTypeChoice == "Titanium":
      balance += 7
    elif resourceTypeChoice == "Diamond":
      balance += 10
    else:
      balance += 0.5
  elif power == 20:
    resourceType = [
      "Coal",
      "Copper",
      "Iron",
      "Gold",
      "Titanium",
      "Diamond",
      "Uranium",
    ]
    resourceTypeChoice = r.choice(resourceType)
    print(resourceTypeChoice)
    if resourceTypeChoice == "Copper":
      balance += 1
    elif resourceTypeChoice == "Iron":
      balance += 2
    elif resourceTypeChoice == "Gold":
      balance += 4
    elif resourceTypeChoice == "Titanium":
      balance += 7
    elif resourceTypeChoice == "Diamond":
      balance += 10
    elif resourceTypeChoice == "Uranium":
      balance += 50
    else:
      balance += 0.5
  else:
    resourceType = ["Coal"]
    resourceTypeChoice = r.choice(resourceType)
    print(resourceTypeChoice)
    balance += 0.5


def pickaxe():
  global balance
  global power
  global cost
  global pickaxePower
  pickaxeType = [
    "Copper Pickaxe",
    "Iron Pickaxe",
    "Drill",
    "Laser Drill",
    "Nuke bomb",
    "Destroyer",
  ]
  pickaxeTypeChoice = r.choice(pickaxeType)
  if pickaxeTypeChoice == "Copper Pickaxe":
    cost = 10
    pickaxePower = 2
  elif pickaxeTypeChoice == "Iron Pickaxe":
    cost = 50
    pickaxePower = 5
  elif pickaxeTypeChoice == "Drill":
    cost = 150
    pickaxePower = 8
  elif pickaxeTypeChoice == "Laser Drill":
    cost = 350
    pickaxePower = 12
  elif pickaxeTypeChoice == "Nuke bomb":
    cost = 700
    pickaxePower = 15
  else:
    cost = 8000
    pickaxePower = 20
  print(pickaxeTypeChoice, cost)
  if cost <= balance:
    costans = input("Buy?(yes/no)")
    if costans == "yes":
      balance -= cost
      power = pickaxePower
    if costans == "no":
      s.system("clear")


def heal():
  global health
  print("Heal store")
  if balance >= 20:
    ans1 = input("Wanna buy a heal +15 for 20? (yes/no)")
    if ans1 == "yes":
      health += 15
    if ans1 == "no":
      s.system("clear")
  if balance >= 50:
    ans1 = input("Wanna buy a heal +60 for 50? (yes/no)")
    if ans1 == "yes":
      health += 60
    if ans1 == "no":
      s.system("clear")
  if balance >= 125:
    ans1 = input("Wanna buy a heal +200 for 125? (yes/no)")
    if ans1 == "yes":
      health += 60
    if ans1 == "no":
      s.system("clear")


def lottery():
  global costs
  global balance
  costs = 100
  if balance >= costs:
    print()
    print("Lottery cost", costs)
    mult = input("Want to buy a lottery?(yes/no)")
    if mult == "yes":
      balance -= 100
      lotteryType = [
        "150",
        "Drill",
        "Nothing...",
        "10",
        "Nothing...",
        "Nothing...",
        "Nothing...",
        "Nothing...",
        "Nothing...",
        "JACKPOT 777 coins!",
        "gYqjso124%81j",
      ]
      lotteryTypeChoice = r.choice(lotteryType)
      if lotteryTypeChoice == "150":
        print("You recieved 150 coins!")
        balance += 150
        costs *= 1.3
        input()
        s.system("clear")
      if lotteryTypeChoice == "Drill":
        print("You recieved Drill!")
        power = 8
        costs *= 1.5
        input()
        s.system("clear")
      if lotteryTypeChoice == "Nothing...":
        print("You recieved nothing...")
        input()
        s.system("clear")
      if lotteryTypeChoice == "10":
        print("You recieved 10 coins!")
        balance += 10
        costs *= 1.2
        input()
        s.system("clear")
      if lotteryTypeChoice == "JACKPOT 777 coins!":
        print("You recieved JACKPOT 777 coins!")
        balance += 777
        costs *= 3
        input()
      if lotteryTypeChoice == "gYqjso124%81j":
        print("Use it as a password.")
        input()
        s.system("clear")
  if balance >= 10000:
    print()
    ans1 = input("You recieved a mystery gift...(yes for open)")
    if ans1 == "yes":
      balance -= 10000
      end = input("You recieved a password! Jy991NerLLa. Good luck!")
      if end == "":
        s.system("clear")


def gameLoop():
  global balance
  global power
  global randomMonster
  global health
  global monsterTypeChoice
  while True:
    print("----------")
    resources()
    monsterType = ["Spider", "Goblin", "Zerk", "Bat", "Ghost", "Dragon"]
    monsterTypeChoice = r.choice(monsterType)
    randomMonster = r.randint(0, 10)
    if randomMonster == 1:
      print(monsterTypeChoice)
      if monsterTypeChoice == "Spider":
        mHP = 5
        mP = 5
        print("The spider is attacking you!", mHP, "HP", mP, "Power")
        if mHP > 0:
          health -= mP
        if health <= 0:
          print("You are dead...")
          exit()
      if monsterTypeChoice == "Bat":
        mHP = 10
        mP = 13
        print("The bat is attacking you!", mHP, "HP", mP, "Power")

        if mHP > 0:
          health -= mP
        if health <= 0:
          print("You are dead...")
          exit()
      if monsterTypeChoice == "Dragon":
        mHP = 100
        mP = 20
        print("The dragon is attacking you!", mHP, "HP", mP, "Power")

        if mHP > 0:
          health -= mP
        if health <= 0:
          print("You are dead...")
          exit()
      if monsterTypeChoice == "Ghost":
        mHP = 999
        mP = 1
        print("The ghost is attacking you!", mHP, "HP", mP, "Power")

        if mHP > 0:
          health -= mP
        if health <= 0:
          print("You are dead...")
          exit()
      if monsterTypeChoice == "Goblin":
        mHP = 20
        mP = 10
        print("The goblin is attacking you!", mHP, "HP", mP, "Power")

        if mHP > 0:
          health -= mP
          if balance >= 50:
            balance -= 50
        if health <= 0:
          print("You are dead...")
          exit()
      if monsterTypeChoice == "Zerk":
        mHP = 120
        mP = 40
        print("The zerk is attacking you!", mHP, "HP", mP, "Power")
        if mHP > 0:
          health -= mP
          health -= 5
        if health <= 0:
          print("You are dead...")
          exit()
    print("----------")
    print("Health-", health)
    print("Power-", power)
    print("Balance-", balance)
    print("----------")
    shop = input()

    if shop == "h":
      heal()
    if shop == "s":
      pickaxe()
    if shop == "l":
      lottery()
    s.system("clear")
    if shop == "1":
      for i in range(10):
        resources2()
        s.system("clear")
    if shop == "10":
      for i in range(100):
        print("Wait...")
        resources2()
        s.system("clear")
    if shop == "100":
      for i in range(1000):
        print("Wait...")
        resources2()
        s.system("clear")
    if shop == "1000":
      for i in range(10000):
        print("Wait...")
        resources2()
        s.system("clear")
    if shop == "hack":
      s.system("clear")
      ans2 = input("Login-")
      if ans2 == "Miner" or ans2 == "miner":
        print("Cheking.")
        t.sleep(1)
        s.system("clear")
        print("Cheking..")
        t.sleep(1)
        s.system("clear")
        print("Cheking...")
        t.sleep(1)
        s.system("clear")
        print("Cheking.")
        t.sleep(1)
        s.system("clear")
        print("Cheking..")
        t.sleep(1)
        s.system("clear")
        print("succsesful!")
        t.sleep(2)
        s.system("clear")
        ans = input("Write password...")
        if ans == "Jy991NerLLa":
          print("Cheking.")
          t.sleep(1)
          s.system("clear")
          print("Cheking..")
          t.sleep(1)
          s.system("clear")
          print("Cheking...")
          t.sleep(1)
          s.system("clear")
          print("Cheking.")
          t.sleep(1)
          s.system("clear")
          print("Cheking..")
          t.sleep(1)
          s.system("clear")
          print("Done!")
          t.sleep(2)
          s.system("clear")
          balance += 999999
          health += 999999
        elif ans == "gYqjso124%81j":
          print("Cheking.")
          t.sleep(1)
          s.system("clear")
          print("Cheking..")
          t.sleep(1)
          s.system("clear")
          print("Cheking...")
          t.sleep(1)
          s.system("clear")
          print("Cheking.")
          t.sleep(1)
          s.system("clear")
          print("Cheking..")
          t.sleep(1)
          s.system("clear")
          print("What? How? Cheater..")
          t.sleep(2)
          s.system("clear")
          power = 20
        else:
          print("Cheking.")
          t.sleep(1)
          s.system("clear")
          print("Cheking..")
          t.sleep(1)
          s.system("clear")
          print("Cheking...")
          t.sleep(1)
          s.system("clear")
          print("Cheking.")
          t.sleep(1)
          s.system("clear")
          print("Cheking..")
          t.sleep(1)
          s.system("clear")
          print("Cheking...")
          t.sleep(2)
          s.system("clear")
          print("Inncorect!!:((")
          t.sleep(3)


gameLoop()

To do that, you should use a while mHP > 0 loop around each monster fight, and have some code to allow the player to fight back.
Your code and code style is quite messy, but there is also a huge issue of code duplication which you should probably address.

1 Like

Also here is some solution to fix code-duplication in resources function

Here is two dictionary-like lists, one is key and one is value. Using list is to use your r.choice()

resourceType = [
      "Coal",
      "Copper",
      "Iron",
      "Gold",
      "Titanium",
      "Diamond",
      "Uranium"
    ] #this is key (ores)
resourceBalance = [
      0.5,
      1,
      2,
      4,
      7,
      10,
      50
    ] #this is value(balance added)

So you can now assign the power to amount of ores available, such as using a real dictionary

resourcePower = {
      "2":2, #format of dictionary is {key}:{value}, where key can only be string
      "5":3, #the value is used for slicing later
      "8":4,
      "12":5,
      "15":6,
      "20":7
    }

Have these 3 dependent and independent variables set, you no longer need a lot of ‘if’ statements that uses many time and space, instead you can shorten it to:

def resources():
  global balance
  resourcePower = {
      "2":2, #format of dictionary is {key}:{value}, where key can only be string
      "5":3, #the value is used for slicing later
      "8":4,
      "12":5,
      "15":6,
      "20":7
    }
  resourceType = [
      "Coal",
      "Copper",
      "Iron",
      "Gold",
      "Titanium",
      "Diamond",
      "Uranium"
    ] #this is key (ores)
  resourceBalance = [
      0.5,
      1,
      2,
      4,
      7,
      10,
      50
    ] #this is value(balance added)

  #here is 3 methods to do it(almost the same), please choose one and delete the other 2
  
  #method using a lot of variables for debugging
  resourceType = resourceType[:resourcePower.get(str(power), 1)]
  #slice all type of resources into the resource available according to the power
  #the dict.get() is to get the power(all if statements) and use 1 instead if the power isnt in the dict(else statement)
  resourceTypeChoice = r.choice(resourceType)
  balance += resourceBalance[resourceType.index(resourceTypeChoice)
  #value[key.index(item)] is just dict.get() but for lists

  #one liner
  balance += resourceBalance[resourceType.index(r.choice(resourceType[:resourcePower.get(str(power), 1)]))]

  #more tidy one-liner (that doesn’t use resourceType as it isn’t used as output in ur code)
  balance += r.choice(resourceBalance[:resourcePower.get(str(power), 1)])

Hope this help improve your code, as you mention in the title

Also many last-item in your list declarations have the comma for the last item, it will cause EoF error as python is seeking the next item from your comma, please fix this is a serious problem

P.s: This entire code is written here without any tool help, indentation error may happen and minor typo may be experienced, this code is untested, please debug on your own

2 Likes

It is perfectly fine to have a comma after the last item in lists, tuples etc.,
it will not cause any errors.

4 Likes

In fact, it is sometimes necessary (for tuples).

2 Likes

Thanks, I will try to do something with it!

Oh… It’s too hard for me right now… Thanks for helping tho, now I know more about theese functions! I’ll try to do something with it.

incorrect. It can be of any type

1 Like

I’ve never used lists in python before. I probably should.

@HenryMiles3 lists and dictionaries are very powerful it makes it so you you can create things like Morse code converters or other things easily without a ton of variables.

1 Like