Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make this piece of Python code work?

I am making a project, on Replit, that makes money, not in real life. I want the ‘wheat_seads’, ‘melon_seeds’, ‘pumpkin_seeds’, ‘wheat’, ‘melon’, ‘pumpkin’ to be available in and out of functions and be changed in different functions. I tried to put these variables in and out of the ‘farm_command’ to remove the ‘Syntax error’, but it doesn’t work. If you can also, improve this piece of code…

from replit import db


#This function is when 'help' command is run
def help_command():
  print("So you need help...")
  print("I am happy to help!")

  print("1. 'help'")
  print("2. 'farm'")
  print("3. 'harvest'")

#This functon is when 'farm' command is run
def farm_command():
  global wheat_seeds = 1
  global melon_seeds = 1
  global pumpkin_seeds = 1
  global wheat = 0
  global melon = 0
  global pumpkin = 0

  
  print("What seed do you want to farm?")
  farm_seed = input("Seed: ")
  farm_seed = farm_seed.lower()

  if farm_seed == "wheat_seeds":
    print("You planted " + wheat_seeds + " wheat seed/s")
    wheat_seeds -= wheat_seeds
    
    
  


#The user's cash amount
db["User"] = {
  "wallet":0,
  "bank":0
}

#This is the users inventory
inventory = [wheat_seeds, melon_seeds, pumpkin_seeds, wheat, melon, pumpkin]

#This is prompted to ask the user what command should be run
command = input("Command: ")

#This checks which command is inputted and runs the respective command
if command == "help":
  help_command()
elif command == "farm":
  farm_command()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can’t assign a value directly when you defining a global variable.
Try it like this:

global wheat_seeds
wheat_seeds = 1
and so on…

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading