Skip to content
Advertisement

How do I make my stash function work? Every time I write “stash” it responds with “You have 0 Diamonds”. Although I have ran mine several times

This is my code:

import random

Mine_time = 0
Mining = 0
Diamond = 0

def Mine(Mining):
    if Doing == "mine" or Doing == "Mine":
        Mining = random.randint(0, 10)
        print("you mined", Mining,"diamonds ")
    
def Stash(Mining, Diamond):
    if Doing == "Stash" or Doing == "stash":
      Diamond = Diamond + Mining
      print("You have", Diamond, "diamonds")

def Time(Mine_Time):
    if Doing == 'Time' or Doing == 'time':
        print('Your Time is:', Mine_time)

def MT(Mine_Time):
    if Mine_time > 0:
        print("this action is on cooldwon please wait", Mine_time, "seconds")


while True:
    Doing = input("n")
    Mine(Mining)
    Stash(Mining, Diamond)
    Time(Mine_time)
    MT(Mine_time)

The possible commands are Stash, Mine, and Time, but time doesn’t do anything yet. It would be very helpfull if every time I run Stash it didn’t show “You have 0 Diamonds”

Advertisement

Answer

Your code has multiple issues, starting with concept and style and ending with the actual fulfillment of the task. I reworked your code so Mine and Stash work logically (leastwise for me).

Disclaimer: the code below is still wrong on multiple points, but I want to post it in this form in order to be understandable for OP.

import random

Mine_time = 0
Mining = 0
Diamond = 0


def Mine():
    global Mining
    if Doing == "mine" or Doing == "Mine":
        Mined = random.randint(0, 10)
        print("you mined", Mined, "diamonds ")
        Mining += Mined


def Stash():
    global Diamond, Mining
    if Doing == "Stash" or Doing == "stash":
        Diamond = Diamond + Mining
        Mining = 0
        print("You have", Diamond, "diamonds")


def Time():
    if Doing == 'Time' or Doing == 'time':
        print('Your Time is:', Mine_time)


def MT():
    if Mine_time > 0:
        print("this action is on cooldown please wait", Mine_time, "seconds")


while True:
    Doing = input("n")
    Mine()
    Stash()
    Time()
    MT()

What I’ve changed:

  • Your global variables are now global inside your functions. So they keep their values between cycle’s loops.
  • Mine now firstly obtain random Mined value, this value shown to the user, and then it’s added to Mining. So if you mined several times, you lose nothing.
  • Stash now resets Mining to 0, so mined once wouldn’t be added to stash several times.

Not sure how Time and MT should work, so I left them untouched.

What you should change (and also learn):

  • General structure. If you have some data united with some logic, most probably it’s a class. You have to learn what it is and how you should use it for tasks like this, and you wouldn’t have such strange situations with variable scopes.
  • Function concept. Maybe you feel some things, but you definitely don’t understand them.
  • Python code style. Here is quite easy to read intro.
Advertisement