如何使这段 Python 代码工作?
How do I make this piece of Python code work?
我正在 Replit 上做一个赚钱的项目,而不是在现实生活中。我希望 'wheat_seads'、'melon_seeds'、'pumpkin_seeds'、'wheat'、'melon'、'pumpkin' 在函数内外可用并在不同的功能。我试图将这些变量放入和取出 'farm_command' 以删除 'Syntax error',但它不起作用。如果你也可以,请改进这段代码...
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()
定义全局变量时不能直接赋值。
像这样尝试:
全球wheat_seeds
wheat_seeds = 1
等等...
我正在 Replit 上做一个赚钱的项目,而不是在现实生活中。我希望 'wheat_seads'、'melon_seeds'、'pumpkin_seeds'、'wheat'、'melon'、'pumpkin' 在函数内外可用并在不同的功能。我试图将这些变量放入和取出 'farm_command' 以删除 'Syntax error',但它不起作用。如果你也可以,请改进这段代码...
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()
定义全局变量时不能直接赋值。 像这样尝试:
全球wheat_seeds
wheat_seeds = 1
等等...